这是我的登录表单代码。我的问题是在陈述是真的之后。不会重定向到主页,而只是在我的索引页面显示主页。
如何在语句为真后在另一页重定向jquery ajax?
HTML代码
<form id="myForm" class="form-group" method="post" action="actions/login_check.php">
<div id="result"></div>
<table class="table-condensed" >
<tr><br>
<td><input type="text" name="username" class="form-control" autofocus="autofocus" placeholder="Username" size="40"></td>
</tr>
<tr>
<td><input type="password" name="password" placeholder="Password" class="form-control"></td>
</tr>
<tr>
<td><button id="submit" class="btn btn-primary form-control">Sign In</button></td>
</tr>
<tr>
<td align="right" style="font-size:14px;"><a href="forgot_password.php">Forgot Password?</a></td>
</tr>
</table>
</form>
PHP代码
$username = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,sha1($_POST['password']));
$empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
$query = mysqli_query($con,"SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."'");
$row = mysqli_num_rows($query);
$getUser = mysqli_fetch_array($query);
if(empty($username) or $password == $empty)
echo "Invalid Email or Password";
elseif($username == $getUser['username'] and $password == $getUser['password']){
session_start();
$_SESSION['username'] = $username;
$online = "UPDATE users SET active = 1 WHERE username = '".$username."'";
mysqli_query($con,$online);
header("Location: ../home.php");
$_SESSION['timestamp'] = time();
}else
echo "Invalid Email or Password";
jQuery Ajax
$("button#submit").click(function(){
if( $("#username").val == "" || $("#password").val == "")
$("div#result").html("Please enter username and password");
else
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(data) {
$("div#result").html(data);
}
);
$("#myForm").submit(function(){
return false;
});
});
答案 0 :(得分:2)
在POST回复中,您可以使用window.location.href
function(data) {
$("div#result").html(data);
window.location.href=<homepageurl>
}
答案 1 :(得分:0)
从jQuery 1.8开始,您可以在成功的jquery .post
之后使用.done重定向用户。
.done(function() {
window.location.href='http://www.successpage.com';
})
即:
$("button#submit").click(function(){
if( $("#username").val == "" || $("#password").val == "")
$("div#result").html("Please enter username and password");
else
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(data) {
$("div#result").html(data);
}).done(function() {
window.location.href='http://www.successpage.com';
});
$("#myForm").submit(function(){
return false;
});
});
答案 2 :(得分:0)
有很多选择 1)您可以指定 window.location.href 要重定向的URL。但有一个问题。
答案 3 :(得分:0)
试试window.location
success: function (response) {
if (response.d == true) {
window.location = "directed_page";
}
},
或尝试使用
window.location.replace()