Here is the form I am currently working on :
http://compquo.com/join_member.php
To understand what I am trying to achieve, if you could simply type two different password - one for password field and nother for re-password field (dong worry filling any other fields) and hit submit, it will tell you password didnot match. But you will notice the paymoves to the right. It is because I am using die() function there.
I am using die() here when username or password is wrong but that is taking me out of the page but I want to display the messsage on the same page where the form exists. (i.e I want to print an error message when something goes wrong in the place of the form).
But if you type the same password twice correctly for those two fields you will see that the confirmation message is printed on the right place which is in place of the form. It's coz I am using echo there instead of die.
I can do it with one if check becuase I can simply use echo. But when there are a number of if statements to be checked the only option i see is die. I got just a vauge idea, like this
Code: Select all
$error = 0
if (!($num_rows))
{
$error = 1;
}
$row= $connector->fetchArray($result);
// check if passwords match
$_POST['password'] = stripslashes($_POST['password']);
$row['password'] = stripslashes($row['password']);
$_POST['password'] = md5($_POST['password']);
//if password doesnot match
if ($_POST['password'] != $row['password'])
{
$error =1
}
if(error=1){
echo "username or password is invalid";
}
.
.
.
}
else { ?>
display form... // [ I want to pritn the error message right here ] In that case is it best to
echo the whole form in php rather than doing it as html ?
<?} //end of else ?>---- original code ------
Code: Select all
if (isset($_POST['submit']))
{
require_once ('../includes/DbConnector.php');
$connector = new DbConnector();
$check = "SELECT user_name, password, user_type FROM user WHERE user_name = '".$_POST['user_name']."'";
$result = $connector->query($check);
$num_rows = mysql_num_rows($result);
if (!($num_rows))
{
die('<center><strong>Username or password is invalid <strong></center>');
}
$row= $connector->fetchArray($result);
// check if passwords match
$_POST['password'] = stripslashes($_POST['password']);
$row['password'] = stripslashes($row['password']);
$_POST['password'] = md5($_POST['password']);
//if password doesnot match
if ($_POST['password'] != $row['password'])
{
die('<center><strong>Username or password is invalid </strong></center>');
}
if($_POST['user_type']!= $row['user_type']){
die('<center><strong>That user type is not valid for this user</strong></center>');
}
/*register session variables and set last login time.*/
$date = date('d,m,y');
$update_login = $connector->query("UPDATE user SET last_login = '$date' WHERE user_name = '".$_POST['user_name']."'");
$_POST['user_name'] = stripslashes($_POST['user_name']);
$_SESSION['user'] = $_POST['user_name'];
$_SESSION['pass'] = $_POST['password'];
$_SESSION['user_type'] = $_POST['user_type'];
header("Location: index.php");
}else {
?>
// display the form
<?
} // end of else
?>