Click to See Complete Forum and Search --> : Check two fields are the same
scottyrob
01-14-2007, 03:15 PM
Hi
How can i check if two fields are the same before subbmiting the form (to the same page)
Just making a password change script
Scott
NightShift58
01-14-2007, 04:29 PM
Here's a sample script to do that. Change field names and $_POST names to suit your existing script.<?php
// At this point, you must be connected to the server/database
IF (isset($_POST['newPassword1']) and isset($_POST['newPassword2']) :
IF ($_POST['newPassword1'] <> $_POST['newPassword2']) :
print "You have not identical passwords in the password change form."
ELSE :
IF ($changePassword) :
$sql = "SELECT `password` FROM UserTable "
$sql .= "WHERE `user_id` = '$user_id' LIMIT 1";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
IF (mysql_num_rows($qry) == 0) :
print "Sorry, this User ID was not found in the database.";
ELSE :
$row = mysql_fetch_assoc($qry);
// Convert $newPassword to whatever encryption is used in the database
// For example, md5()
$newPassword = md5($_POST['newPassword1']);
IF ($newPassword == $row['password']) :
print "Sorry, your new password is the same as the old one.";
ELSE :
// Update the user/pass table with new password
$sql = "UPDATE UserTable SET `password` = '$newPassword' ";
$sql .= "WHERE `user_id` = '$user_id' LIMIT 1";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
print "Your new password has been changed in the database.";
ENDIF;
ENDIF;
ENDIF;
ENDIF;
ENDIF;
?>
bokeh
01-14-2007, 04:36 PM
before subbmiting the formTo check two inputs are identical before submission you would need Javascript.
NightShift58
01-14-2007, 05:47 PM
To check two inputs are identical before submission you would need Javascript.Sorry, I misread the question.
This above comment from Bokeh is correct: it would be best (as was your original intention) to intercept non-identical passwords at the root of all possible evil - directly in the input form - and you should do that. Something like the following:<html>
<head>
<script type="text/javascript">
function validate_passw(thisform) {
with (thisform) {
if (newPassword1.value==null||newPassword1.value=="") {
alert("Please enter a password...");
newPassword1.focus();
return false;
}
if (newPassword2.value==null||newPassword2.value=="") {
alert("Please retype a password...");
newPassword2.focus();
return false;
}
if(newPassword1.value != newPassword2.value) {
newPassword1.value = "";
newPassword2.value = "";
alert("Please enter identical new passwords...");
newPassword1.focus();
return false;
}
}
}
</script>
</head>
<body>
<form name="passchange" action="YOUR_SUBMIT_PAGE.PHP" onsubmit="return validate_passw(this)" method="post">
New password: <input type="text" name="newPassword1" value="" size="30">
<br>
Retype new password: <input type="text" name="newPassword2" value="" size="30">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
However, it must be checked on the processing page as well, as not all browsers are javascript-enabled and a password change is, after all, a critical function.
scottyrob
01-15-2007, 03:01 AM
Ahh thats great, Thanks very much for that!
NightShift58
01-15-2007, 08:52 AM
You're welcome!