[英]ajax login success data but not redirect to index page
i want to validate login form using ajax success response is working but if all goods it should be redirect to index page but it's not working , i don't understand what's wrong please help. 我想使用Ajax成功响应来验证登录表单是否有效,但是如果所有商品都应重定向到索引页面但不起作用,我不知道怎么了,请帮忙。 thanks in advance..
提前致谢..
$(document).ready(function(){
var response;
$('#submit').click(function(e){
e.preventDefault();
$('.alert-box').html('<div id="loading" style="margin: 0 auto;" >
</div>');
var action = 'ajax_validation';
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url:"do_login.php",
method:"POST",
data:{action:action, username:username, password:password},
success:function(data){
response = data;
if(response === 1){
window.location.href = "http://stackoverflow.com";
}else{
$('.alert-box').addClass("alert alert-warning");
$('.alert-box').html(response);
}
}
});
});
});
above these ajax request this is action page code include('includes/db.php'); 在这些ajax请求之上,这是操作页面代码include('includes / db.php');
if(isset($_POST["action"])){
$check_username = $_POST['username'];
$check_password = $_POST['password'];
if(empty($check_username) && empty($check_password)){
echo "Please fill all field";
}else{
$query = "SELECT * FROM user WHERE email = '{$check_username}' AND password = '{$check_password}' ";
$select_query=mysqli_query($connection,$query);
if(!$select_query){
die("QUERY FAILED".mysqli_error($select_query));
}
if(mysqli_num_rows($select_query)==0){
echo "Username or password are incorrect!";
}else{
while ($row=mysqli_fetch_assoc($select_query)) {
$_SESSION['username'] = $row['email'];
echo $row['email'];
}
}
}
}
In your resposne you are echoing 在您的回应中,您正在回声
echo $row['email'];
This should be: 应该是:
echo 1;
Your problem is that you are checking if value is 1 in ajax: 您的问题是您要检查ajax中的value是否为1:
success:function(data){
response = data;
if(response === 1){ //<- there you are checking if your echo is 1 but you are trying to echo $row['email'];
window.location.href = "http://stackoverflow.com";
}else{
$('.alert-box').addClass("alert alert-warning");
$('.alert-box').html(response);
}
}
change your echo from $row['email']
to 1
将您的回声从
$row['email']
更改为1
I think that the problem is response checking condition in ajax success. 我认为问题在于ajax成功中的响应检查条件。 You're checking if a string (email or error message) is equal and the same type of 1.
您正在检查字符串(电子邮件或错误消息)是否相等并且类型相同(为1)。
...
dataType: 'json',
success:function(data){
response = data;
if(response === 1){
...
You can use a json response with status/error code: 您可以使用带有状态/错误代码的json响应:
...
success:function(data){
response = JSON.parse(data);
if(response.status === 1){
window.location.href = "http://stackoverflow.com";
}else{
$('.alert-box').addClass("alert alert-warning");
$('.alert-box').html(response.data);
}
}
...
$check_username = $_POST['username'];
$check_password = $_POST['password'];
$res = array('status'=> 0, 'data' => '');
if(empty($check_username) && empty($check_password)){
$res['status'] = 0;
$res['data'] = "Please fill all field";
}else{
$query = "SELECT * FROM user WHERE email = '{$check_username}' AND password = '{$check_password}' ";
$select_query=mysqli_query($connection,$query);
if(!$select_query){
$res['status'] = 0;
$res['data'] = "QUERY FAILED".mysqli_error($select_query);
echo json_encode($res);
return;
}
if(mysqli_num_rows($select_query)==0){
$res['status'] = 0;
$res['data'] = "Username or password are incorrect!";
}else{
while ($row=mysqli_fetch_assoc($select_query)) {
$_SESSION['username'] = $row['email'];
$res['status'] = 1;
$res['data'] = "".$row['email'];
}
}
}
echo json_encode($res);
In addition i suggest to put only the username in query where condition and check if the password match with php. 另外,我建议仅在查询条件的地方输入用户名,并检查密码是否与php匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.