2012-07-30 63 views
0

我有用於在jQuery mobile上登錄的此代碼。但是,它不起作用,什麼也沒有發生。當我提交時我回到同一頁面。使用jQuery mobile和php登錄

就像他沒有輸入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> 

這裏LoginExcution.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(); 
      } 

謝謝

+0

您無法使用AJAX響應重定向用戶的瀏覽器。做它使用Javascript – 2012-07-30 07:43:03

+0

我搜索谷歌和大多數人這樣做@AlexanderLarikov – 2012-07-30 07:47:30

+0

它不工作,對不對? :) – 2012-07-30 07:49:27

回答

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; 
}); 

另外,我個人寧願像

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

{ status: 'ok', msg: 'logging in...' }一個JSON編碼的結果。

然後,您可以通過切換收到的狀態來決定是否顯示錯誤消息或通過「location.href = ...」重定向到另一個頁面。