2013-06-18 42 views
0

我有一個如下所示的頁面,其中顯示了使用按鈕從數據庫中的用戶記錄列表。當我點擊該按鈕時,會顯示一個jQuery模式表單,其中包含一個允許某人輸入其用戶信息的表單。使用jquery模式形式將數據插入MYSQL

我的問題是當我點擊'添加新用戶'按鈕時,數據應該插入數據庫的模式表單上。但是這些數據不會去數據庫。

這是users.php頁面的代碼與模式窗體對話框

<body> 

    <h1>User Management System</h1> 

    <div id="manage_user"> 
     <form action="" method=""> 

      // Here I display a table with user details................. 

      <button id="FormSubmit">Add New User</button> 
     </form> 
    </div> 

    <div id="dialog" class="new_user_dialog_box" title="Add New User"> 
     <p>Fill this form with your details and click on 'Add New User' button to register.</p> 

     <p class="validateTips">All form fields are required.</p> 

     <div id="new_user_form"> 
      <table> 
       <tr> 
        <td>Name :</td> 
        <td><input type="text" name="name" value="" id="name" /></td> 
       </tr>    
       <tr> 
        <td>Address :</td> 
        <td><input type="text" name="address" value="" id="address" /></td> 
       </tr>    
       <tr> 
        <td>City :</td> 
        <td><input type="text" name="city" value="" id="city" /></td> 
       </tr> 
      </table> 
     </div> 
    </div> 

</body> 

的JavaScript/jQuery腳本是

 if (bValid) { 

      jQuery.ajax({ 
       type: "POST", // HTTP method POST or GET 
       url: "process.php", //Where to make Ajax calls 
       dataType:"text", // Data type, HTML, json etc. 
       data:bValid, //Form variables 
       success:function(response){ 
        //on success, hide element user wants to delete. 
        //$('#item_'+DbNumberID).fadeOut("slow"); 
       }, 
       error:function (xhr, ajaxOptions, thrownError){ 
        //On error, we alert user 
       alert(thrownError); 
       } 
      }); 
      $(this).dialog("close");      
     } 

這裏的任何幫助深表感謝。

謝謝。

+0

點擊按鈕和插入數據庫的數據之間有幾個步驟。哪一步失敗? AJAX調用是對服務器進行的嗎?它是否包含預期的數據?服務器的迴應是什麼? PHP代碼是否按預期執行? JavaScript控制檯中是否有錯誤? PHP日誌中是否有錯誤? – David

+0

@David感謝您的回覆。 JavaScript控制檯中沒有任何錯誤。從模態表單對話框填寫表單字段後,它將關閉,並且不會發生數據傳輸' – TNK

+0

PHP錯誤日誌中也沒有記錄 – TNK

回答

1

看起來你實際上並沒有在你的POST中發送數據。看看你的AJAX調用中的這一行:

data:bValid, //Form variables 

什麼是bValid?從上面這行代碼的外觀來看,它只是一個布爾值。但AJAX POST中的data需要包含與表單元素對應的鍵/值對。嘗試這樣的事情,而不是:

dataType: 'JSON', 
data: { name: $('#name').val(), address: $('#address').val(), city: $('#city').val() } 

這將在您的形式發送鍵/值對的JSON對象,從與IDS nameaddress HTML元素得到他們的價值觀和city

+0

我剛剛試過了,並且出現了一個警告'SyntaxError:JSON.parse:unexpected字符' – TNK

+0

然後我發現數據將進入數據庫。但不能同時顯示在users.php頁面中。如果要顯示數據,我需要重新加載頁面。 – TNK

+0

我可以知道原因嗎?謝謝。 – TNK