我有一個投票功能,它使用AJAX提交用戶投票并更新數據庫,而無需刷新頁面。到目前為止,一切都很好。但我也想從數據庫中檢索更新的值,并在頁面上更新它。我已經將第二個AJAX請求嵌套在我的第一個請求中。第二個請求調用文件new_values.php該文件獲取最新值并將其放入數組中,并返回為 JSON,如下所示 $new_vals = array( 'new_total' => $new_total, 'poll_option_1_val' => $poll_option_1_val, 'poll_option_2_val' => $poll_option_2_val, ); echo json_encode($new_vals);下面是Ajax請求 - 第一個請求可以很好地更新數據庫,但內部AJAX請求不起作用。在下面的示例中,我嘗試使用 alert 來顯示new_total值,但沒有任何反應$(function () { // SUBMIT FORM WITH AJAX $('#poll-form').on('submit', function (e) { //on form submit e.preventDefault(); // prevent default behaviour if($("form")[0].checkValidity()) { // check if the form has been validated $.ajax({ // submit process type: 'post', url: 'vote-process.php', data: $('form').serialize(), success: function () { $('#vote_submitted').modal('show'); $("input").attr("disabled", "disabled"); $("textarea").attr("disabled", "disabled"); $("#vote_button").attr("disabled", "disabled"); $("#vote_button").text("Vote submitted"); $.ajax({ url : 'new_values.php', type : 'POST', data : data, dataType : 'json', success : function (result) { alert(result['new_total']); }, error : function () { alert("error"); } }); }, error: function() { $('#error').modal('show'); } }); return false; } else { // if the form is not valid console.log("invalid form"); } });});這讓我發瘋。任何幫助將不勝感激!
1 回答

GCT1015
TA貢獻1827條經驗 獲得超4個贊
第二個Ajax數據:數據會給你這個問題需要傳遞正確的參數
$.ajax({
url : 'new_values.php',
type : 'POST',
data : {data_return:'yes'},
dataType : 'json',
success : function (result) {
alert(result['new_total']);
},
error : function () {
alert("error");
}
});
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消