2016-07-29 86 views
0

我想通過ajax請求創建登錄頁面,但現在面臨的問題,當用戶輸入錯誤的密碼我希望它仍然在同一頁上。併成功登錄它跳轉到下一個頁。如何重定向多個位置取決於JSON.stringify(響應)

$('#login').on('click',function(event){ 

    event.preventDefault(); 
    var username = document.getElementById("username").value; 
    var password = document.getElementById("password").value; 
    if(password == ''||username == '') 
      { 
       alert ('Please fill the fields!'); 
      } 
      $.ajax({ 
     url: "api/learnapi.php", 
     type: "get", 
     dataType: "json", 
     data: {type: "login",email:username ,pass:password}, 
     //type: should be same in server code, otherwise code will not run 
     ContentType: "application/json", 
     success: function (response) { 
      alert(JSON.stringify(response)); 
      location.href ="app-student-dashboard.php"; 
     }, 
     error: function (err) { 
      alert(JSON.stringify(err)); 

     } 
    }); 

    }); 

但是當輸入正確或錯誤的密碼時,它會跳轉到app-student-dashboard.php,我該如何阻止它這樣做。這裏是我的驗證用戶

$loname = $_GET['email']; 
    $lopass = $_GET['pass']; 

    $query1="select * from signup where email='$loname'"; 
    $result1= mysqli_query($conn,$query1); 
    $row = mysqli_fetch_array($result1, MYSQLI_ASSOC); 
// printf ("%s \n", $row["password"]); 
    $pass1=$row["password"]; 


    if($pass1 == $lopass) { 
     $res["flag"] = true; 
     $rest["message"] = "Login successful."; 
     } 
    if($pass1 != $lopass) 
    { 

    $res["flag"] = false; 
     $rest["message"] = "Wrong password entered."; 

    } 

代碼是這可能調用錯誤:功能(錯誤)從那裏發送消息「輸入錯誤口令」。

回答

1

從@dadan得到幫助後,我只是把裏面的條件成功作爲

success: function (response) { 

       alert(JSON.stringify(response)); 
        if(response.flag == true){ 
          location.href ="app-student-dashboard.php"; 
            } 
     }, 

得到了預期的結果。

0

,我認爲你應該將檢查的成功裏面,使條件如果響應== true,那麼重定向頁面..

+0

我如何使成功的條件。我嘗試如果(響應==真正),而不是重定向到下一頁 – oceanier

+0

@oceanier就是這樣,給我投票,如果它有幫助。謝謝! – dadan