JQuery登录没有响应

时间:2013-07-29 07:24:03

标签: php jquery html

我是Jquery的新手,我正在尝试创建一个登录页面,使用JQuery和PHP,但是当我点击登录按钮时它没有响应,我想要的是登录成功后它重定向到index.html。

这是JQuery代码:

<script type="text/javascript" > 
$(document).ready(function() {
  $("form#memberlogin").submit(function(e) {
      dataString = $(this).serialize();
      $.post("check_login.php", dataString, function(data) {
          $('form#memberlogin').hide();
          window.location="index.html"
      });
      e.preventDefault(); 
  });
});
</script>

这是我的表单代码:

<form id="memberlogin" class="form-horizontal"  method="post">
                    <fieldset>
                        <div class="input-prepend" title="Username" data-rel="tooltip">
                            <span class="add-on"><i class="icon-user"></i></span><input id="username" autofocus class="input-large span10" name="username" id="username" type="text" value="admin" />
                        </div>
                        <div class="clearfix"></div>

                        <div class="input-prepend" title="Password" data-rel="tooltip">
                            <span class="add-on"><i class="icon-lock"></i></span><input id="password" class="input-large span10" name="password" id="password" type="password" value="admin123456" />
                        </div>
                        <div class="clearfix"></div>

                        <div class="input-prepend">
                        <label class="remember" for="remember"><input type="checkbox" id="remember" />Remember me</label>
                        </div>
                        <div class="clearfix"></div>

                        <p class="center span5">
                        <button type="submit" class="btn btn-primary">Login</button>
                        </p>
                    </fieldset>
                </form>

这是我的PHP代码:

 <?php
 ob_start();
 $con=mysql_connect(localhost,"root","-----");
 if(!con)
 {
  die(mysql_error());
  exit();
 }

 mysql_select_db("sure") or die(mysql_error());

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

if(get_magic_quotes_gpc()){
  $user = stripslashes($user);  //mencegah mysql injection
  $pass = stripslashes($pass);
}
  $user = mysql_real_escape_string($user);
  $pass = mysql_real_escape_string($pass);

  $result=mysql_query("SELECT * FROM user WHERE username='$user' and password='$pass'") or  die(mysql_error());
  $count=mysql_num_rows($result);
  if($count==1)
  {
    session_register("user");
    session_register("pass");
    $_SESSION['username']=$_POST['user'];
    $_SESSION['id']=mysql_result($result,0,'id');

    echo 'correct';
    header("location:index.html");
  }
  else
  {
    die("id atau password anda salah");
    exit();
  }

  mysql_close($con);
   ob_end_flush();
 ?>

2 个答案:

答案 0 :(得分:0)

尝试使用此代码。

if($count==1)
{
session_register("user");
session_register("pass");
$_SESSION['username']=$_POST['user'];
$_SESSION['id']=mysql_result($result,0,'id');

echo 'Y';
//header("location:index.html");
}
else
{
die("id atau password anda salah");
exit();
}

在javascript中:

$.post("check_login.php", dataString, function(data) {
      // data is the response sent from the check_login.php

      // display the type of data
      console.log( typeof data );
      console.log( data );

      // if data is Y
      var reg = /Y/g;

      // regex to check the result
      if(  reg.test(data) ){

         console.log('IN');
         $('form#memberlogin').hide();

         //top.location.href = "index.html";

      } else {
         console.log('OUT');
         // PHP dies message here.
         //alert(data);
      }

      return false;
  });

尝试在firebug控制台选项卡中检查响应。

答案 1 :(得分:0)

尝试使用href之类的,

window.location.href="index.html"

阅读此What's the difference between window.location= and window.location.replace()?