1

我是 Jquery 的新手,我正在尝试使用 JQuery 和 PHP 创建一个登录页面,但是当我单击登录按钮时它没有响应,我想要的是登录成功后它重定向到索引。 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();
 ?>
4

2 回答 2

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 控制台选项卡中的响应。

于 2013-07-29T07:32:13.810 回答
0

尝试使用hreflike,

window.location.href="index.html"

阅读此window.location= 和 window.location.replace() 有什么区别?

于 2013-07-29T07:33:12.377 回答