Reputation: 109
I get the data through ajax then I validate it and redirect user as per condition. Like if user login successful then redirect to dashboard otherwise redirect to restrict page. I get the result data in network console(developers tool) but I am unable to redirect to specific page. Here is my index.php page.
<div id="loginContainer">
<form id="loginFrm" method="post">
<h3>Members Login Area</h3>
<img id="divider" src="images/divider.png">
<input type="email" name="lemail" required placeholder="Enter a valid email address">
<input name="lpassword" placeholder="Password" type="password" required>
<input type="submit" value="Sign In">
<input id="submit" type="hidden" name="submit">
<p><a href="#">Forgot Password ?</a></p>
<p id="bottom">Don't have account? <a href="#" id="signup">Sign up here</a></p>
</form>
</div>
<script>
$(document).ready(function () {
$("#loginFrm").submit(function () {
var data = $("#loginFrm").serialize();
insertRecords(data);
return false;
});
function insertRecords(data) {
$.ajax({
url: 'loginProcess.php',
data: data,
type: 'POST',
dataType: 'html',
success: function (data) {
$("#result").html(data);
},
error: function () {
alert('Error : COde not work properly');
}
});
}
});
</script>
This is my loginProcess.php page where i get the data from users input.
<?php
ob_start();
error_reporting(E_ALL);
print_r($_POST);// see all result in network console
require_once('inc/dbconnection.php');
require_once('inc/functions.php');
$emailErr = $password="";
if (isset($_POST['submit'])) {
if (empty($_POST["lemail"])) {
$emailErr = 'Please enter your email address.';
} else {
$email = filterEmail($_POST["lemail"]);
if ($email == FALSE) {
$emailErr = 'Please enter a valid email address.';
}
}
if (empty($_POST["lpassword"])) {
$passwordErr = 'Please enter your password here.';
} else {
$password = sha1($_POST['lpassword']);
}
}
// after pressing login, checking if the variables exist in the database
if (empty($emailErr) && empty($passwordErr)) {
$query = $db->prepare("SELECT password FROM users WHERE email=?");
$query->execute(array($email));
if ($query->fetchColumn() === $password) {
// starts the session created if login info is correct
session_start();
$_SESSION['email'] = $email;
header("Location: dashboard.php",true, 302);
exit;
} else {
header("Location: restrict.php",true, 302);
exit;
}
} else {
header("Location: index.php?Email=$emailErr&Password=$passwordErr",true, 302);
die;
}
ob_end_flush();
?>
Note: i've checked after remove the (true, 302) in header function
Upvotes: 1
Views: 236
Reputation: 608
Are you sure you should be redirecting from your PHP form processing script? I believe you should just generate the ERROR/NO ERROR response their and redirect the page from your form page by handling the AJAX response cases.
Upvotes: 0
Reputation: 175
The AJAX request will fail if the page you're fetching the result from does not contain data in a format expected by the ajax()
Javscript function (caused by a redirected result page).
Why not pass the URL from PHP to the Javascript function, check if the URL to redirect to is set then redirect accordingly instead?
window.location.replace('dashboard.php');
Upvotes: 0
Reputation: 2277
You cannot redirect in php
with AJAX
. You need to redirect in success
function of AJAX
like below:
window.location.href = 'dashboard.php'
Upvotes: 1