【发布时间】:2018-04-07 10:22:21
【问题描述】:
我通过 ajax 获取数据,然后验证它并根据条件重定向用户。就像用户登录成功然后重定向到仪表板,否则重定向到限制页面。我在网络控制台(开发人员工具)中获得了结果数据,但我无法重定向到特定页面。 这是我的 index.php 页面。
<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>
这是我的 loginProcess.php 页面,我从用户输入中获取数据。
<?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();
?>
注意:我在删除标题函数中的 (true, 302) 后检查过
【问题讨论】:
标签: php jquery html ajax redirect