2013-07-11 166 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, 检查了会议。