2012-09-21 45 views
1

我有一個php腳本,通過他們的電子郵件和密碼驗證用戶,如果它是無效的我的腳本回應像「無法登錄」,如果它是成功,它將直接到另一個頁面,我該怎麼做?如何根據下面的情況使用jquery ajax指向另一個頁面?

這裏是我的PHP代碼:

if(isset($_POST['email'])&&isset($_POST['password'])){ 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    $result = userAuthentication($email,$password); 

    if($result == false){ 
     echo 'Unable to login'; 
    } 
    else if($result == true){ 
     header("location: success.php"); 
    } 
} 

這裏是我的js代碼:

$(function() { 
    $("button").button(); 
    $("#clickme").click(function(){ 
     $.post("check.php", { 
      "email": $("#txtEmail").val(), 
      "password": $("#txtPassword").val() 
     }, 
     function(msg){ 
      $(".message").html(msg); 
     }); 
     return false; 
    }); 
}); 
+1

做客戶端:'document.location.replace(NEW_LOCATION)' – Nemoden

+0

這是最好使用window.location還是那個? :) 謝謝! –

回答

2

你不能從PHP重定向這樣。您可以返回成功的消息,並從JavaScript重定向:

PHP:

if(isset($_POST['email'])&&isset($_POST['password'])){ 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    $result = userAuthentication($email,$password); 

    if($result == false){ 
     echo 'Unable to login'; 
    } 
    else if($result == true){ 
     echo 'success'; 
    } 
} 

的javascript:

$(function() { 
    $("button").button(); 
    $("#clickme").click(function(){ 
     $.post("check.php", { 
      "email": $("#txtEmail").val(), 
      "password": $("#txtPassword").val() 
     }, 
     function(msg){ 
      $(".message").html(msg); 
      if(msg == 'success'){ 
       window.location = 'success.php'; 
      } 
     }); 
     return false; 
    }); 
}); 
+0

謝謝,它的工作原理! :) –

相關問題