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 6688735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:15+00:00 2026-05-26T05:26:15+00:00

I wrote the beginnings of a very simple comment system. It uses jQuery /

  • 0

I wrote the beginnings of a very simple comment system. It uses jQuery / AJAX / PHP MySQL. So far it works fine. But, once a comment is submitted you have to refresh the page to show the comment. How can it show on submit.

I hope this isn’t too much code here, but here it is in three parts. The jQuery / the php insert comment query / the php select comments query.

jQuery / AJAX:

$(document).ready(function() {
  $('#button').click(function() {
    var name = $('#name').val();
    var comment = $('#comment').val();

    if(name == '' || comment == '') {
        $('#comment_messages').html('Please enter both fields');
    } else if(name !== '' || comment !== '') {
        $('#comment_messages').html('');

    $.ajax({
    type: 'POST',
    url: 'comments.php',
    data: 'name='+name+'&comment='+comment,
    success: function(data) {
    $('#comments_area').append(data);
    }
    });
    }
  });
});

PHP INSERT (to insert comments):

<?php

include('init.inc.php');

if(isset($_POST['name'], $_POST['comment'])) {
  $name = $_POST['name'];
  $comment = $_POST['comment'];
  if(!empty($name) && !empty($comment)) {
    $query = mysql_query("INSERT INTO comments VALUES(NULL, '$name', '$comment', CURRENT_TIMESTAMP)");
    if($query === true) {

      // right here is what is being returned to success: function(data) in the ajax script.  What's the best way to return the comment here? 

    } else {
      echo 'Hmmm... that\'s odd........';
    }
  } else {
    echo 'Please enter both fields';
  }
}

?>

PHP SELECT (to retrieve comments):

<?php

$query = mysql_query("SELECT * FROM comments ORDER BY time DESC LIMIT 10");

  $num = mysql_num_rows($query);
  if($num >= 1) {
    while($fetch = mysql_fetch_assoc($query)) {
      $name = $fetch['name'];
      $comment = $fetch['comment'];
      $time = $fetch['time'];

      ?>

        <div id="user_comments">

          <?php echo $name; ?> said at: <span id="time_stamp"><?php echo $time; ?></span><p>- <?php echo $comment; ?>

        </div>

      <?php

    }
}

?>

UPDATE:

Added two lines at bottom:

$(document).ready(function() {
  $('#button').click(function() {
    var name = $('#name').val();
    var comment = $('#comment').val();

    if(name == '' || comment == '') {
        $('#comment_messages').html('Please enter both fields');
    } else if(name !== '' || comment !== '') {
        $('#comment_messages').html('');

    $.ajax({
    type: 'POST',
    url: 'comments.php',
    data: 'name='+name+'&comment='+comment,
    success: function(data) {
    $('#comments_area').append('<b>'+name+'</b><p>- '+comment);
    $('#comment_messages').html(data);
    }
    });
    }
  });
});
  • 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-26T05:26:16+00:00Added an answer on May 26, 2026 at 5:26 am

    You already have the comment when you send it, so when the AJAX call to submit the comment returns a successful, you can just append the comment without actually getting it from PHP/AJAX.

    So don’t return the comment, that’s just data which isn’t actually going to be used.

    $.ajax({
    type: 'POST',
    url: 'comments.php',
    data: 'name='+name+'&comment='+comment,
    success: function(data) {
    $('#comments_area').append('<b>'+name+'</b>: '+comment);
    }
    });
    }
    });
    

    And return a 200 statuscode in your PHP file if succes, or a 4xx statuscode if fail. This way only when a 200 statuscode is returned, it will go into the succes: block

    Instead of appending the comment like above, you could also use jQuery templates ( http://api.jquery.com/template/).

    Filter by statuscode too: (doc on this page): http://api.jquery.com/jQuery.ajax/

    Catch failure like in example below:

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        // add comment here, succes will go here if ajax returns a 200 statuscode
      }
      error: function(){
        // add failure here, error will go there if ajax returns a 4xx statuscode
      }
    });
    

    If you want to use the statuscodes, take a look at this example:

    $.ajax({
      statusCode: {
        200: function() {
            //succes
        },
        400: function(){
            // bad request
        }
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple batch file as a PowerShell script, and I am getting
I'm working on the beginnings of porting my php based OOP web framework to
Okay, here is a very simple and fundamental question. If I have an application
I can't find the answer on SO but it's very likely that the argument
I've written 1 WPF app. A pretty simple app. It works great and is
I am trying to analyze a geotiff file, very simple, just trying to get
I'm just beginning to learn python. I wrote an example script to test OOP
I just want to output current and I wrote import java.util.*; at beginning, and
Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate

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.