I currently have a html login page which has two textfields for e-mail address and password. The HTML contains a form and a button which triggers some javascript to check the data input is valid. If the data is valid, then the data is posted to a php application.
This php application gives a response dependent on whether the login details were valid/invalid.
Here is my php:
if(isset($_POST['username']) && isset($_POST['password'])){
//connect to database
$dbh = connect();
$user = mysql_real_escape_string($_POST['username']);
$usertype = mysql_real_escape_string($_POST['usertype']);
$pass = md5($_POST['password']);
$query = "";
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $user)) {
if($usertype == "SupportStaff"){
$query = "SELECT staffId, fName, lName, gender, email FROM SupportStaff WHERE email = '$user' AND password = '$pass'";
}else{
$date = $_POST['currdate'];
$query = "SELECT athleteId, fName, lName, gender, email FROM Athletes WHERE email = '$user' AND password = '$pass'";
}
//make query
$result = mysql_query( $query ) or die ("didn't query");
//see if there's an EXACT match
$num = mysql_num_rows( $result );
if ($num == 1){
$row = mysql_fetch_assoc($result);
if($usertype == "Athlete"){
$user = str_replace("@","at",$user);
$user = str_replace(".","dot",$user);
$user .= "Entries";
$query = mysql_query("SELECT * FROM $user WHERE date = '$date'");
$exists = false;
if(mysql_num_rows( $query ) == 1){
$exists = "true"; //to see if questionnaire is complete
}else{
$exists = "false";
}
$row['complete'] = $exists;
}
echo json_encode($row);
} else {
echo ($user." ");
echo ($pass);
echo ("&result=invalid");
}
}else{
mysql_close($dbh);
echo ("&result=false"); //invalid e-mail address
}
mysql_close($dbh);
}
?>
If it echos &result=false OR &result=invalid then I would like the user to be served up with the login page again displaying an error message or something similar, if it is successful (echo json_encode($row)) then it should take to home.html (i.e. the homepage)...What is the best way to achieve this?