Thursday, October 5, 2023
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  112] [ 3]  / answers: 1 / hits: 17924  / 15 Years ago, mon, february 23, 2009, 12:00:00

I have a site where each user's page shows comments and allows other user's to add comments. I want to have it so the add comments form is on the page and when a user adds a comment, it is added to the database and shows up in the comment section with AJAX. I am using jQuery for the AJAX and LINQ to SQL to handle the database logic. How would go about doing this so that after the comment is added to the database, the comments section is refreshed and updated without refreshing the page?


More From » jquery

 Answers
15

You would need to take advantage of the 'success' (or 'complete') event that is fired by the jQuery ajax call to fire a subsequent AJAX call for refreshing the content of your reviews. This would probably look something like (winged it, untested):



function UpdateComments(){
resultHTML = jQuery.ajax({
type: 'GET',
url: 'Comments/List/UserID'
}).responseText;

$('#comments').html(resultHTML);
}

function PostComment(targetUserID, commenterUserID, comment)
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: $.toJSON({review: comment, id:targetUserID, commenter:commenterUserID}),
dataType: 'json',
url: 'Comments/Add',
success: function(result){
// Only update comments if the post was successful:
resultJson = $.evalJSON(result);
if(resultJson['success'] == true){
UpdateComments();
}
}
});


EDIT The JSON code would make use of the jquery plugin jquery-json (http://code.google.com/p/jquery-json/)


[#99940] Tuesday, February 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blair

Total Points: 384
Total Questions: 108
Total Answers: 86

Location: Northern Ireland
Member since Tue, May 5, 2020
3 Years ago
;