2014-01-20 55 views
0

評論表正在提交,並且數據也被保存到數據庫中。但不刷新頁面,不會在瀏覽器上顯示。如何顯示評論表單提交而無需使用AJAX刷新頁面?

這裏是代碼:

$("#post_reply").click(function (event) { 
    $("#data_status").html(""); 
    $('#ajax_loading').show(); 
    event.preventDefault(); 
    if (document.getElementById('_comment').value.trim() == "") { 
     return false; 
    } 
    $.post('../services/leave_comment.php', $("#open_status").serialize(), function (data) { 
     $('#ajax_loading').hide(); 
     if (data.split("::")[1] == true) { 
      $("#data_status").html("Commented Successfully.."); 
      $("#data_status").fadeOut(3000); 
      document.getElementById('_comment').value = ''; 
      $('#_comment').html(""); 

     } else if (data.split("::")[1] == false) { 
      $("#data_status").html("Error occured in Comment Submission.. TryAgain.."); 
      $("#data_status").fadeOut(3000); 
     } 
    }); 
}); 

編輯:

所有我能理解是我沒有發表與阿賈克斯的數據? 這是我需要做的?

$("#post_reply").click(function (event) { 
$("#data_status").html(""); 
$('#ajax_loading').show(); 
event.preventDefault(); 
if (document.getElementById('_comment').value.trim() == "") { 
    return false; 
} 
$.post('../services/leave_comment.php', $("#open_status").serialize(), function (data) { 
    $('#ajax_loading').hide(); 
    if (data.split("::")[1] == true) { 
     $("#data_status").html("Commented Successfully.."); 
     $("#data_status").fadeOut(3000); 
     document.getElementById('_comment').value = ''; 
     $('#_comment').html(""); 


$.ajax({ 
type: 'POST', 
url : 'http://localhost/tech1/services/get_more_comments.php', 
data: 'last_id='+last_id, 
success: function(data){ 
$('.view_container').append(data); 
}, 
complete: function(){ 
console.log('DONE'); 
        }      
       }); 

    } else if (data.split("::")[1] == false) { 
     $("#data_status").html("Error occured in Comment Submission.. TryAgain.."); 
     $("#data_status").fadeOut(3000); 
    } 
}); 
}); 
+0

不顯示怎麼辦? –

+0

@GertB。不顯示評論。它發生在我刷新頁面查看評論。 – stacky

+2

我在你的代碼中沒有看到你添加評論到你的頁面 –

回答

0

您的所有代碼都會將數據發佈到服務器。沒有任何東西可以從服務器獲取新評論,也可以手動追加發布的評論。您可以再次使用ajax來刷新評論,或者更簡單地使用發佈的內容追加評論。

0

我要說的搜索jQuery的.load網站:

例如:

function updateShouts(){ 
    // Assuming we have #shoutbox 
    $('#shoutbox').load('latestShouts.php'); 
} 
在這種情況下 shoutbox將與您的評論包含div

, 你會呼籲成功這個功能你的ajax帖子 latestshouts.php只會包含該div的內容。

有點難以解釋,我希望這對你有意義

鏈接:http://api.jquery.com/load/

相關問題