2016-02-26 45 views
0

負載數據不能夠插入和使用負載數據的代碼jQuery的AJAX插入並從數據庫

$.ajaxSetup({ 
    // Disable caching of AJAX responses 
    cache: false 
}); 




$(function() { 

    $("#submit").click(function() { 
     var test = $("#txtarea").val(); 
     var dataString = 'question=' + test; 
     if (test == '') { 
      alert("<h3>Please Enter Some Text</h3>"); 
     } else { 
      $("#loading").show(); 
      $("#loading").fadeIn(400).html('<span class="loading">Loading Comment...</span>'); 
      $.ajax({ 
       type: "POST", 
       url: 'q&a.php', 
       data: dataString, 
       cache: false, 
       error: function() { 
        $('#loading').html('<p>An error has occurred</p>'); 
       }, 
       success: function(html) { 
        $("#cm").after(html); 
        document.getElementById('txtarea').value = ''; 
        document.getElementById('txtarea').focus(); 
        $("#load").hide(); 
       } 
      }); 
     } 
     return false; 
    }); 

}); 
+0

更多有關該問題的更多細節 – guradio

+0

試圖插入數據並使用此jquery ajax代碼獲取數據但不工作。 –

+0

我正在嘗試做相同的計算器'add comment'部分的作品 –

回答

0

你需要URL編碼的數據值,如果它包含特殊字符。最簡單的方法是使用對象而不是字符串; jQuery將自動對其進行編碼。

var datastring = { question: test }; 

順便說一句,這通常不需要使用cache: falsetype: "POST"。瀏覽器不應緩存POST響應,只有GET響應。

另外,在您的success函數中,您隱藏了錯誤的ID。它應該是$("#loading").hide()

+0

現在代碼工作正常。感謝Barmar先生 –