i am trying to refresh the page upon successful login in the Ajax script but the page is not refreshed. I am forced to refresh the page manually and only then will it appear to be logged in. Thanks in advance for any help, it is much appreciated.
This is the login script for ajax;
$("document").ready(function() {
$("#what").click(function() {
var email = $("#emailLogin").val();
var password = $("#passwordLogin").val();
var dataString = 'email='+email+'&password='+password;
$.ajax({
type: "POST",
url: "authenticate.php",
data: dataString,
success: function(response){
//alert (response);
if (response == 0) {
$("#problem").html("Please enter correct details");
} else {
window.location = "index.php";
}
}
});
return false;
});
});
This is the php script for validating the form
// authenticate.php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$user_id = credentials_valid($_POST['email'], $_POST['password']);
if($user_id){
log_in($user_id);
if($_SESSION['redirect_to']){
header("Location: " . $_SESSION['redirect_to']);
unset($_SESSION['redirect_to']);
}else{
header("Location: index.php");
}
}else{
echo "0";
}
?>
Of course you need to refresh the page because you post with JQuery and the php function only returns some data.
You have 2 alternatives:
Post the classic way (form submit)
Redirect from javascript using window.location=’url’;