Building a comment system with Ajax and JQuery and I want the div the comments are in to reload after a comment is added. It posts just fine. This is what I have so far.
The function getComments queries the database and generates the html
$.ajax({
type: "POST",
url: "post_comment.php",
data: dataString,
cache: false,
success: function(html){
????????? What should go here... it is a div with id#commentBody
}
<div id="commentBody">
<ul>
<?php
$q = "SELECT * FROM comment WHERE parent_id = 0 AND idpost = $postID";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
getComments($row,$postID,$custID);
endwhile;
?>
</ul>
</div>
When you post it, you should return the data you want from your server side script. Then you can use the
.html()jQuery function to update your div.So, like:
You could also return just the latest comment (optionally as a JSON object) and then just use the
.append()method to add it to your #commentBody.I would create a JSON object which has a
statusproperty and adataproperty. When thestatusis -1 (or whatever) there was an error adding the comment and you could put a message in thedataproperty. When thestatusis 0, it was successful and the latest comment information would be available available in thedataproperty.Example
PHP
JavaScript