2014-09-24 109 views
-1

我不知道這裏有什麼問題。 preventDefault應該停止提交表單,但仍然繼續。我有一個ajax調用,它驗證用戶是否有效。如果不是,請阻止提交。否則,繼續登錄和主頁。表格仍然提交甚至與preventDefault

<form id="signIn" method="post" action="processForms.php"> 
    <table cellspacing="10"> 
     <tr id="errorSignIn" hidden="hidden"> 
      <td class="centerItem errorMessage" colspan="3"> 
       Incorrect Username and/or Password 
      </td> 
     </tr> 
     <tr> 
      <td><input type="text" id="username" name="username" autocomplete="off" autofocus required placeholder="Username..."></td> 
      <td><input type="password" id="password" name="password" autocomplete="off" required placeholder="Password..."></td> 
      <td><input type="submit" name="processButton" class="signIn" value="Sign-in" ></td> 
     </tr> 
    </table> 
</form> 

的Javascript

$('#signIn').submit (function (e) { 
    var username = $('#username').val(); 
    var password = $('#password').val(); 
    var dataString = "username=" + username + "&password=" + password; 
    $.ajax({ 
     type: "POST", 
     url: "ajaxCheck.php", 
     data: dataString, 
     cache: false, 
     success: function (result) { 
      if (!result) { 
       $('#errorSignIn').removeAttr('hidden'); 
       e.preventDefault(); 
       return false; 
      } 
     } 
    }); 
}); 

ajaxCheck.php

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 
$password = md5($password); 
$dbConnection = mysqli_connect('localhost','root','','onboard'); 
$query = "SELECT * FROM account WHERE username='$username' AND password='$password'"; 
$result = mysqli_query($dbConnection,$query); 
$count = mysqli_num_rows($result); 
if ($count == 1) { echo true; } 
else { echo false; } 
+0

您只是在內部函數返回false,但外部函數沒有返回值,所以提交將被解僱。 – Tyr 2014-09-24 01:43:38

+0

@Tyr我試着添加「return false;」到另一個函數並向內部函數添加else子句以返回true,但現在當用戶存在時它不會提交表單。 – 2014-09-24 02:02:23

+0

您現在的代碼已打開[** SQL注入**](http://stackoverflow.com/q/60174/)。使用[** CRYPT_BLOWFISH **](http://security.stackexchange.com/q/36471)或PHP 5.5的['password_hash()'](http://www.php.net/manual/en/) function.password-hash.php)函數。對於PHP <5.5,使用['password_hash()兼容包]](https://github.com/ircmaxell/password_compat)。另外,[**使用準備好的語句**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php)或[**使用準備好的語句**的PDO](http ://php.net/pdo.prepared-statements)。 – 2014-09-24 02:13:34

回答

4

因爲你是丘比特在ajax成功函數內的預防默認是異步的。所以這就是爲什麼它仍然提交。你必須在調用ajax之前而不是在ajax完成之前阻止它。

$('#signIn').submit (function (e) { 
     e.preventDefault(); // put preventDefault here not inside the ajax success function 
     var username = $('#username').val(); 
     var password = $('#password').val(); 
     var dataString = "username=" + username + "&password=" + password; 
     $.ajax({ 
      type: "POST", 
      url: "ajaxCheck.php", 
      data: dataString, 
      cache: false, 
      success: function (result) { 
       if (!result) { 
        $('#errorSignIn').removeAttr('hidden'); 

        return false; 
       } 
      } 
     }); 
    }); 

在這裏回答您的關注問題是您可以做的事情。

var isFormChecked = false; // this variable will deremine if the ajax have finished what you wanted to do 
$('#signIn').submit (function (e) { 

// if ajax set this to true, it will not go here when it triggers. 
if(!isFormChecked){ 
    e.preventDefault(); // put preventDefault here not inside the ajax success function 
    var username = $('#username').val(); 
    var password = $('#password').val(); 
    var dataString = "username=" + username + "&password=" + password; 
    $.ajax({ 
     type: "POST", 
     url: "ajaxCheck.php", 
     data: dataString, 
     cache: false, 
     success: function (result) { 
      // if result is set to true 
      isFormChecked = result; 
      // then trigger the submit of the form 
      $('#signIn').trigger('submit'); 

     } 
    }); 
} else { 
    // form is submitted 
    isFormChecked = true; 
} 
}); 
+0

當ajax結果設置爲true時,如何繼續提交表單?我嘗試解綁然後提交,但它沒有提交。 – 2014-09-24 02:00:39

+0

當我造成一個無限循環時,我感到困惑,我注意到我把函數中的var isFormChecked。感謝你的回答。 :) – 2014-09-24 02:28:26

0

你可以做的是把你的html中的按鈕類型從提交改爲按鈕。

然後點擊驗證,然後在jQuery的幫助下提交表單。

這裏是它如何做你的工作:

<form id="signIn" method="post" action="processForms.php" > 
    <table cellspacing="10"> 
     <tr id="errorSignIn" hidden="hidden"> 
      <td class="centerItem errorMessage" colspan="3"> 
       Incorrect Username and/or Password 
      </td> 
     </tr> 
     <tr> 
      <td><input type="text" id="username" name="username" autocomplete="off" autofocus required placeholder="Username..."></td> 
      <td><input type="password" id="password" name="password" autocomplete="off" required placeholder="Password..."></td> 
      <td><input type="button" name="processButton" class="signIn" value="Sign-in" ></td> 
     </tr> 
    </table> 
</form> 



$('#processButton').click (function (e) { 
    var username = $('#username').val(); 
    var password = $('#password').val(); 
    var dataString = "username=" + username + "&password=" + password; 
    $.ajax({ 
     type: "POST", 
     url: "ajaxCheck.php", 
     data: dataString, 
     cache: false, 
     success: function (result) { 
      if (!result) { 
       $('#errorSignIn').removeAttr('hidden'); 
       return false; 
      } 
      else { $("#signIn").submit(); } 
     } 
    }); 
});