2013-07-11 76 views
-2

我有jQuery登錄表單,該表單的功能是檢查用戶驗證。如果成功驗證jQuery表單驗證重定向

這裏是JS:

$(document).ready(function() 
    { 
     $('#button_sign_in').click(function(){ 
      console.log('login'); 
      $.ajax({ 
       url: "login", 
       type: "post", 
       data: $('#login_form').serialize(), 
       success: function (data) { 
        //console.log('data:'+data); 
        if (data.user) { 
         $('#lblUsername').text(data.user.username); 
        } 
        else { 
         $('#button_sign_in').shake(4,6,700,'#CC2222'); 
         $('#username').focus(); 
        } 
       }, 
       error: function (e) { 
        console.log('error:'+e); 
       } 
      }); 
     }); 
    }); 

的邏輯是,如果錯了用戶名或密碼,則該按鈕會動搖。如果正確的登錄然後重定向到主頁。

,這裏是我的PHP函數:

require ("functions/_db_.php"); 

$username = $_POST['username']; 
$password = $_POST['password']; 

$query = mysql_query("SELECT username, password FROM t_users WHERE username='$username' && password='$password'"); 
$found = mysql_num_rows($query); 
if ($found > 0) 
{ 
    header('location:home.php'); 
} 

現在的問題是:如果登錄糾正,頁面將不會重定向到主頁。

有什麼建議嗎?

回答

0

保持你的PHP腳本作爲波紋管

 //Check your inputs against to mysql injections 
     $username = mysql_real_escape_string($_POST['username']); 
     $password = mysql_real_escape_string($_POST['password']); 

     $query = mysql_query("SELECT username, password FROM t_users WHERE username='$username' && password='$password'"); 
     $found = mysql_num_rows($query); 


     if ($found > 0) 
     { 
      $res='SUCCESS'; 
     } 
     else 
     { 
      $res='ERROR'; 
     } 

     $ary=array("status"=>$res); 

     echo json_encode($ary); //Json output - we are sending the assynchronus data in json format 

和腳本

$.ajax 
({ 
    url: "login", 
    type: "post", 
    data: $('#login_form').serialize(), 
    dataType:'json', //We need ajax data in json type 
    success: function (data) 
    { 
     if (data.status=='SUCCESS') //status is proper of the json object, which we gave in php code in above script 
     { 
      window.location='home.php'; 
     } 
     else 
     { 
      $('#button_sign_in').shake(4,6,700,'#CC2222'); 
      $('#username').focus(); 
     } 
    }, 
    error: function (e) { 
     console.log('error:'+e); 
    } 
}); 
+0

哇......非常感謝@Haraman。現在工作。 :) –

0
if (data.user) 
{ 
    $('#lblUsername').text(data.user.username); 
    window.location = "home.php"; 
} 
1

也許是因爲你沒有做重定向本身?

success: function (data) { 
        //console.log('data:'+data); 
        if (data.user) { 
         $('#lblUsername').text(data.user.username); 
         window.location = '/home.php'; // redirect to the homepage 
        } 
        else { 
         $('#button_sign_in').shake(4,6,700,'#CC2222'); 
         $('#username').focus(); 
        } 
       } 

如果通過AJAX將其發送到客戶端,使用標頭的PHP重定向將不起作用。在這種情況下,您必須在客戶端使用JS重定向它。

+0

我試過了,現在還在工作。 –

0

老兄,我想你在PHP方面有問題,而不是JavaScript。 考慮到這一點,這裏是您的解決方案,而不是位置是位置

header('Location: http://www.example.com/'); 

Document

+0

嗨,我嘗試過,並沒有工作... –

+0

第一:請告訴我你獲得價值$發現? 第二:你是否收到任何警告或通知,如果是這樣,給我發消息 –

+0

這就是爲什麼,也許我錯過了什麼或必須發送價值從$發現到ajax成功?那裏沒有錯誤。你可以在http://bootply.com/tagged/modal看到演示,點擊登錄 –

0

當你可以通過AJAX調用一個php腳本,調用header()將不會在瀏覽器中加載新頁面。您可以通過echo 'success/fail'響應AJAX調用,並通過檢查AJAX的success:部分中的響應來自行重定向到所需的頁面。不要忘了設置php會話,並檢查登錄後要顯示的頁面中用戶登錄是否成功。所以,你的PHP代碼中會,

if ($found > 0) { 
    //set session here 
    echo 'success'; 
} 
else 
    echo 'fail'; 

和阿賈克斯,

success: function (data) { 
    if (data == 'success') { 
    window.location = "home_page.php"; 
    } 
    else { 
    $('#button_sign_in').shake(4,6,700,'#CC2222'); 
    $('#username').focus(); 
    } 
} 

在你hompage PHP, 檢查了會議。