2015-09-30 69 views
1

我有登錄表格的問題。當我嘗試登錄時,即使在輸入正確的用戶名和密碼時,始終返回false值。檢查密碼是否正確jquery

這裏是我的jQuery的文件代碼:

$(document).ready(function(){ 

var teamname = $("#teamname"); 
var teampassword = $("#teampassword"); 

function isPasswordCorrect() 
{ 
    var password = teampassword.val(); 
    var name = teamname.val(); 
    var result = false; 

    $.post('../php/validations/validatePassword.php', {tname: name, tpassword: password}, function(data){ 
     if(data == 1){ 
      result = true; 
     }else{ 
      result = false; 
     } 
    }); 
    return result; 
} 

$("#join").click(function(){ 
    if(isPasswordCorrect()){ 
     alert("You have joined"); 
    }else{ 
     alert("You have not joined"); 
    } 
}); 
}); 

這是我在PHPfile代碼:

<?php 
$teamname = $_POST['tname']; 
$teampassword = $_POST['tpassword']; 

if($teamname != "" || $teampassword !=""){ 
    include('../connection.php'); // Here is the log in to the phpmyadmin 

    $queryCheckPassword = mysqli_query($con, "SELECT password FROM 
    teams WHERE name = '$teamname'"); 

    $row = mysqli_fetch_row($queryCheckPassword); 
    $teamPassword = $row[0]; 
    if($teamPassword == $teampassword) 
    { 
     echo 1; 
    }else{ 
     echo 0; 
    } 
} 
?> 

這裏是我在HTML文件中的代碼:

<form id="joinform"> <!-- action="teams.php" method="post"> --> 
    <ul> 
     <div> 
      <label>Team name&nbsp<font color='red'>*</font></label> 
      <input type='team' name='teamname' id='teamname' placeholder='Team name' readonly='readonly'/> 
      <span id='teamnameinfo'>Select an existing team</span> 
     </div> 
     <div> 
      <label for="teampassword">Team password&nbsp<font color='red'>*</font></label> 
      <input type='password' name='teampassword' id="teampassword" placeholder='Team password'/> 
      <span id='teampasswordinfo'>Write team password</span> 
     </div> 
     <div> 
      <button name='join' id='join'>Join</button> 
     </div> 
    </ul> 
</form> 
+2

** A ** JAX是**異步** - 「結果」變量操作不太準確。請在AJAX回調中進行檢查。 – PeterKA

+2

我希望代碼不在互聯網上。如果是這樣,你應該添加一些代碼來防止SQL注入。 –

+2

你真的應該使用PHP的[內建函數](http://jayblanchard.net/proper_password_hashing_with_PHP。html)來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。這裏是一個例子[使用AJAX的密碼測試](http://jayblanchard.net/putting_it_all_together.html) –

回答

0

PeterKA大約是正確的異步。默認值(false)可能在異步調用返回之前返回。嘗試添加回調(未經測試):

function isPasswordCorrect(callback) 
{ 
    var password = teampassword.val(); 
    var name = teamname.val(); 

    $.post('../php/validations/validatePassword.php', {tname: name, tpassword: password}, 
      function(data) { callback(data == 1); }); 
} 

$("#join").click(function(){ 
    isPasswordCorrect(function(result) { 
     if (result) 
      alert("You have joined"); 
     else 
      alert("You have not joined"); 
    }); 
}); 
+0

非常感謝! –

1

問題在於你使用Javascript。看看你的isPasswordCorrect函數(我稍微重新格式化,以使問題更清晰一點):

function isPasswordCorrect() 
{ 
    var password = teampassword.val(); 
    var name = teamname.val(); 
    var result = false; 

    $.post(
     '../php/validations/validatePassword.php', 
     {tname: name, tpassword: password}, 
     function (data) { 
      if(data == 1){ 
       result = true; 
      }else{ 
       result = false; 
      } 
     } 
    ); 

    return result; 
} 

請參閱function (data) {}在那裏?這是一個回調。該$.post方法的工作原理是這樣的:

  1. 您的JS代碼發送到使用$.post

  2. 你的JS代碼的服務器請求繼續,移動到下一個

  3. 一段時間無論發生什麼事以後(可能是10毫秒後,可能是30秒後),服務器響應請求。 那時你的回調叫做

這是什麼意思?當您的代碼碰到return result;行時,您的代碼剛將請求提交給服務器,並且服務器可能尚未響應。因此,result仍然是false

迅速解決這個問題對於alert語句進入回調,像這樣:

function isPasswordCorrect() 
{ 
    var password = teampassword.val(); 
    var name = teamname.val(); 

    $.post(
     '../php/validations/validatePassword.php', 
     {tname: name, tpassword: password}, 
     function (data) { 
      if(data == 1){ 
       alert("You have joined"); 
      }else{ 
       alert("You have not joined"); 
      } 
     } 
    ); 
} 

$("#join").click(function(){ isPasswordCorrect(); }); 

不過,我想你會想要做的不僅僅是alert更多的東西。你需要研究JavaScript的異步特性並理解它,然後才能擴展這個片段來做很多事情。

+0

非常感謝! –

0

因爲AJAX不會立即返回結果 - 異步 - 你想要做的檢查只有只有當你有結果 - 在AJAX回調,像這樣:

function isPasswordCorrect() 
{ 
    var password = teampassword.val(); 
    var name = teamname.val(); 
    $.post(
     '../php/validations/validatePassword.php', 
     { tname: name, tpassword: password }, 
     function(data) { 
      if(data == 1) { 
       alert("You have joined"); 
      } else { 
       alert("You have not joined"); 
      } 
     } 
    ); 
} 

$("#join").click(isPasswordCorrect);