Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4002160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:59:32+00:00 2026-05-20T07:59:32+00:00

Building a comment system with Ajax and JQuery and I want the div the

  • 0

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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:59:32+00:00Added an answer on May 20, 2026 at 7:59 am

    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:

    $('#commentBody').html(html);
    

    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 status property and a data property. When the status is -1 (or whatever) there was an error adding the comment and you could put a message in the data property. When the status is 0, it was successful and the latest comment information would be available available in the data property.

    Example

    PHP

    //Check postback variables, add comment and retrieve
    //  comment information (such as ID) if necessary
    if (postedsuccessfully) {
       $ary = array("status" => 0,
                    "data" => array("id" => $commentidvar,
                                    "user" => $commentuser,
                                    "text" => $comment)
                   );
       echo json_encode($ary);
    } else {
       $ary = array("status" => -1,
                    "data" => "There was a problem adding your comment.");
       echo json_encode($ary);
    }
    

    JavaScript

    success: function(json){
       if (json.status == 0) {
          $mydiv = $('<div>');//construct your comment div using json.data.id,
                              //json.data.user, and json.data.text
          $('#commentBody').append($mydiv);
       } else {
          alert(json.data);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm building an Article / Comment system like this: class Article(models.Model): title
I am building a commenting system where people can comment on uploaded files, messages
I am building a comment system. so, I have a php page that displays
I am building a simple ranking system. When a comment is voted up, the
Background I'm writing an asynchronous comment system for my website, after reading plenty of
I'm building a commenting system with PHP/MySQL. This commenting system has a nice feature,
Looking at the general trend of comments in my question about Building an Aircraft
I'm currently designing a comments table for a site I'm building. Users will be
Building an inventory system. I have lots of products and each product has three
I'm reading a book (Rails 3 in Action) that's building a project management system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.