簡體   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