繁体   English   中英

ajax登录成功数据,但不重定向到索引页面

[英]ajax login success data but not redirect to index page

我想使用Ajax成功响应来验证登录表单是否有效,但是如果所有商品都应重定向到索引页面但不起作用,我不知道怎么了,请帮忙。 提前致谢..

$(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);
              }
                }
            });
       });
    });

在这些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'];
            }
        }
    }

  }

在您的回应中,您正在回声

 echo $row['email'];

应该是:

 echo 1;

您的问题是您要检查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);
      }
 }

将您的回声从$row['email']更改为1

我认为问题在于ajax成功中的响应检查条件。 您正在检查字符串(电子邮件或错误消息)是否相等并且类型相同(为1)。

...
dataType: 'json',
success:function(data){
              response = data;
              if(response === 1){
...

您可以使用带有状态/错误代码的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);

另外,我建议仅在查询条件的地方输入用户名,并检查密码是否与php匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM