2016-02-25 75 views
-3

請注意,即時通訊嘗試從index.php/html跳轉到test.php頁面使用ajax和mysql,如果在表中找不到文本輸入,那麼它應該去test.php else留在阿賈克斯警報的index.php/html頁面上,但是從索引頁,每次接收不可用,有時提交按鈕不起作用,下面的代碼FYR ...ajax不返回到PHP索引頁

//index.php $(document).ready(function() { 
      //default form not submitted 
      $("#submit").data("submitted",false);  
      //preventing enter key in input field from submitting form 
      $("#welcome").on("submit", function(e){ 
       if($('#submit').data("submitted")===false) { 
       e.preventDefault(); 
       } 
      }); 
      //trigger form submission 
      $("#submit").on("click",function(){ 
      validate(); 
      });}); 
//default form not submitted     
//$("#submit    
function validate() { 
       var num = $('#invst_num').val(); 
       $.ajax({ 
       type: 'POST', 
       url: 'check_test.php', 
       data: num, 
       cache: false, 
       dataType: "json", 
       success: function(data) { 
         if(data){ 
          alert("NOT AVAILABLE"); 
         } else { 
          $("#submit").data("submitted", true); 
          $("#welcome").submit(); 
          } 
         }}</script> <form action="check_test.php" method="post" name="welcome" id="welcome" /> <table width="550" border='0' align='center' cellpadding='0' cellspacing='0'> <tr> 
<td align="center"><label> 
    Enter Inv. # *: 
    <input name="invst_num" type="text" id="invst_num" size="40" /> 
    <input name="submit" type='submit' id='submit' /> </label></td> </tr></table> </form> 

//check_test.php <?php 
include ("config/config.php"); 
//get the username 
if (isset($_POST['submit'])) { 
$invst_num = $_POST['invst_num']; 
//mysql query to select field username if it's equal to the username that we check ' 
$sql = mysql_query("select invst_num_ar from shareholders_ar where invst_num_ar = '$invst_num' "); 
if ($result = mysql_num_rows($sql)>0) { 
    echo ($result); 
    } 
} 
?> 
// if not found...test.php should load 
<html> 
<form 
... 
Register Data 
/form> 
</html> 
+0

**警告**:如果您只是學習PHP,請不要學習過時的'mysql_query'界面。這很糟糕,並且已經在PHP 7中被刪除。像[PDO這樣的現代替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo - 用於數據庫訪問/)。像[PHP The Right Way](http://www.phptherightway.com/)這樣的指南可以幫助解釋最佳實踐。因爲你有嚴重的[SQL注入漏洞](http://bobby-tables.com/),所以一定要絕對**確定**你的用戶參數[妥善轉義](http://bobby-tables.com/php)在這個代碼中。 – tadman

回答

0

你的條件是向後

if(data){ 

應該是

if(!data){ 

或者警報應該隨監聽器添加邏輯而翻轉。

+0

謝謝lwnnay :)我想知道條件語句更改爲'if(!data){',但是當編譯顯示防止功能警告和表單的開發工具時,會在下一個** index.php **頁面時雙擊提交按鈕如果聲明爲false,則不提示。 – virtualyyours

0
var num = $('#invst_num').val(); 
$.ajax({ 
     type: 'POST', 
     url: 'check_test.php', 
     data: num, 

ajax調用發送值frome data鍵,所以你只發送文本字段的內容。在瀏覽器中

$.ajax({ 
     type: 'POST', 
     url: 'check_test.php', 
     data: { 'invst_num': num }, 
... 

開放開發工具和檢查Ajax調用時正在發送的內容:

你或許應該把某事像這樣。

+0

感謝您的回答,我也改變了'數據',現在仍然彈出警告不允許去索引頁面,進一步我指出,雖然單擊提交按鈕DEV工具顯示以下兩條警告消息 使用// @指示sourceMappingURL編譯指示已棄用。使用//#代替jquery.min.js:4:0 不推薦使用getPreventDefault()。改爲使用defaultPrevented。 jquery.min.js:2:8283 – virtualyyours

+0

正如@Iwnnay所說,這種情況是錯誤的 - 您正在等待接收數據,但是如果您發送警報。從我的網站幾個通知:使用http響應代碼來表明是否有任何錯誤,所以在這種情況下200是好的,400錯了。依靠這個在js中驗證它。欲瞭解更多信息,請看[這裏](http://php.net/manual/en/function.http-response-code.php) 你仍然沒有寫什麼數據被髮布和迴應是什麼 - 在你的開發工具可以啓用像「保存日誌」一樣的東西,並檢查真正發生的事情, – kolemp