2017-10-15 60 views
-1

一個簡單的登錄表單我有一個登錄表單是這樣的: 我有3個文件:index.htmllogin.phpcustom.js創建AJAX,PHP,jQuery的

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Login</title> 
    </head> 
    <body>  
    <section> 
     <div id="message"></div>  
     <form id="loginform" method="POST"> 
     <p>Username: <input type="text" id="username" name="username" size=25 /> 
     </p> 
     <p>Password: <input type="password" id="password" name="password" size=25 /> 
     </p> 
     <button id="login" type="submit" name="submit">Login</button> 
     </form>  
    </section> 
    </body> 
</html> 

custom.js看起來是這樣的:

$('#login').click (function() { 
    $.ajax({ 
    type: "POST", 
    url: 'login.php', 
    data: $('#loginform').serialize(), 
    dataType: 'json', 
    success: function(data) { 
     //alert(data.message); 
     // print into (<div id="message"></div>) on file html.    
    }, 
    }); 
    return false; 
}); 

login.php看起來這樣:

$users = array(
    0 => array(
    "username" => "admin", 
    "password" => "admin", 
), 
    1 => array(
    "username" => "demo", 
    "password" => "demo", 
), 
); 

請檢查下面我的代碼:

for ($i=0; $i< count($users) ; $i++){ 
    if (isset($_POST['username'])&& isset($_POST['password'])) { 
    // Please check this code 
    if ($_POST['username'] == $users[$i]['username'] && $_POST['password'] == $users[$i]['password']) { 
     $data = array('result' => 'success','message' => 'Message sent from server'); 
     echo json_encode($data); 
     // Please check this code 
    } else { 
     // Please check this code 
     $data = array('result' => 'fail', 'message' => 'Message not sent from server'); 
     echo json_encode($data); 
    } 
    } 
} 

我想從表單的登錄用戶可以檢查用戶(陣列),使用AJAX打印消息,如果匹配用戶與否。

請幫我檢查我的所有代碼並修復它。謝謝大家。

+2

是否有你忘了介紹了一些實際問題呢? 「請檢查我的代碼」並不是一個真正的可回答的問題。 – David

回答

0

我不確定我是否理解你,我的意思是帶有i +的部分......但是......你不能只是查詢表格:query roucount where user = @_post [user] AND password = @ _post [pass],然後如果mysqli rowcount == 1登錄,否則die + error?

+0

沒有用的mysql。我想用$ _POST ['username'],$ _ POST ['password']檢查登錄信息。我如何編碼? –

0

問題是什麼?

$i=0; $i< count($users) ; $i++) 對我來說看起來像一個bug。

嘗試$ i < = count $ users。

+0

我想用$ users數組比較$ _POST ['username'],$ _ POST ['password']。我如何編碼? –

0

首先,您的custom.js需要檢查結果是否成功。

$('#login').click (function() { 
    $.ajax({ 
     type: "POST", 
     url: 'login.php', 
     data: $('#loginform').serialize(), 
     dataType: 'json', 
     success: function(data) { 
      // Check the result from data 
      if (data.result == 'success') { 
       $('#message').html('success'); 
      } else { 
       $('#message').html('fail'); 
      } 
     }, 
     error: function(d) { 
      $('#message').html('error'); 
     } 
    }); 
    return false; 
}) 

其次,你的login.php返回所有的JSON即使登錄成功與否,所以應該更新喜歡這個

for ($i=0; $i< count($users) ; $i++){ 
    if (isset($_POST['username'])&& isset($_POST['password'])) { 
     if ($_POST['username'] == $users[$i]['username'] && $_POST['password'] == $users[$i]['password']) { 
      $data = array('result' => 'success', 'message' => 'Message sent from server'); 
      echo json_encode($data); 
      // Add return to prevent echo multi json result 
      return; 
     } 
    } 
} 

// Move the fail code outside the loop otherwise will keep echo fail json 
$data = array('result' => 'fail', 'message' => 'Message not sent from server'); 
echo json_encode($data); 
return; 

這只是在你的代碼的簡單解決方案的基礎上,存儲PHP中未加密的密碼不是最好的解決方案。

全碼 https://gist.github.com/stanleybz/ad795c599d9db68e0e28bf6d979ae190

+0

我運行你的代碼,但它總是報告失敗,即使我輸入了正確的用戶信息。請幫幫我。 –

+0

您是否嘗試過從我的要點完整的代碼?你可以檢查是PHP返回正確的JSON類似於'{'result':'fail','message':'Message not sent from server'}'from a console.log(data)'in ajax –

+0

Yep 。我試過完整的代碼。它沒有運行。當我將它複製到我的代碼時,它只顯示失敗。 –