0

我有这段代码用于登录 jQuery mobile。但是,它不起作用,什么也没有发生......当我提交时,我返回到同一页面。

就像他不输入php文件一样。

这是ajax脚本:

   <script>
     $(document).ready(function(){
    $("#loginform").submit( function(){
    $.post('LoginExcution.php',
    $(this).serialize(), function(data){ $("#errorm").html(data)}};
    return false;
    }); 
</script>

这是html:

<div data-role="page" id="login">
    <div data-theme="a" data-role="header">
</div>
<div data-role="content" style="padding: 15px">
    <div style="text-align:center">
        <img style="width: 50%; height: 50%" src="logo.jpg" />
    </div>
    <form id="loginform" method='post'>
                <div data-role="fieldcontain">
                    <fieldset data-role="controlgroup">
                        <label for="textinput2" style="text-align:right">
                           Email:
                        </label>
                        <input id="textinput2" name="login" value="" type="text"/>
                    </fieldset>
                </div>
                <div data-role="fieldcontain">
                    <fieldset data-role="controlgroup">
                        <label for="textinput3" style="text-align:right">
                            Password:                            </label>
                        <input id="textinput3" name="password" value="" type="password"/>
                    </fieldset>
                </div>     
                <h3 id="errorm"> <?php if (isset($_GET['msg'])){ 

          echo "Invalid username or password";    
 } 


 ?></h3>
                <input type="submit" name="submit" id="submit" data-inline="true" data-  icon="arrow-l" data-iconpos="left" value="login"/>
            </form>

这里是LoginExecution.php:

                    //Sanitize the POST values
$login = $_POST['login'];
$password = $_POST['password'];

         $aqry="SELECT * FROM admin WHERE name='$login' AND password='".$_POST['password']."'";

           $employeeresult=mysql_query($eqry);  
            $adminresult=mysql_query($aqry);
    if( $employeeresult || $adminresult ){
     //Check whether the query was successful or not



                  if(mysql_num_rows($adminresult) == 1) {
        //Login Successful

        $member = mysql_fetch_assoc($adminresult);
        $_SESSION['MEMBER_ID'] = $member['AdID'];
        $_SESSION['NAME'] = $member['name'];


        header("location: mlogin.php");
        exit();
        }  
                            else { 
             //Login failed 
         header("Location: mobile/mlogin.php?msg=invalid%20name%20or%20password"); 
              exit(); 
             } 

谢谢

4

1 回答 1

2

你的脚本搞砸了,试试这个:

$(document).ready(function() {
  $("#loginform").submit(function() {
    $.post('LoginExcution.php', $(this).serialize(), function(data) { 
      $("#errorm").html(data); // semicolon missing in your code
    }); // round bracket and semicolon missing in your code
  }); // round bracket missing in your code
  return false;
}); 

另外,我个人更喜欢 JSON 编码的结果,例如

{ status: 'error', msg: 'some message text' }

或者

{ status: 'ok', msg: 'logging in...' }.

然后,您可以通过切换接收状态来决定是显示错误消息还是通过“location.href = ...”重定向到另一个页面。

于 2012-08-10T12:24:10.690 回答