[英]How to redirect page after the jquery ajax is call?
This is my Login form code. 这是我的登录表单代码。 My problem is after the statement is true.
我的问题是陈述正确之后。 Is will not be redirect to the home page instead it's just show the home page at my index page.
Is不会重定向到主页,而是仅在我的索引页上显示主页。
How to Redirect jquery ajax at the other page after the statement is true? 语句为true后,如何在另一页重定向jquery ajax?
HTML Code 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 code 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 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;
});
});
In the POST response, you can use window.location.href
在POST响应中,可以使用
window.location.href
function(data) {
$("div#result").html(data);
window.location.href=<homepageurl>
}
As of jQuery 1.8, you can use .done to redirect the user after a successful jquery .post
. 从jQuery 1.8开始,可以在成功使用jquery
.post
之后使用.done重定向用户。
.done(function() {
window.location.href='http://www.successpage.com';
})
ie: 即:
$("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;
});
});
There are plently of options. 有很多选择。
1) You can assign window.location.href the URL which you want to redirect.
1)您可以分配window.location.href您要重定向的URL。 But there is a problem.
但有一个问题。
Just try window.location 只需尝试window.location
success: function (response) {
if (response.d == true) {
window.location = "directed_page";
}
},
or try to use 或尝试使用
window.location.replace()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.