Nested comment system in PHP, AJAX

This tutorial shows how to create nested or hierarchical comment system in PHP, MySQL, jQuery. This threaded comment accepts reply up to maximum of five level depth. If you want to accept unlimited level or customize level of depth then you can modify according to your needs.

Create MySQL table comment. You can use phpMyAdmin or SQLyog to run the below SQL and it will automatically create a table for you.

You may also read Nested Comments using Codeigniter, AJAX

Create MySQL table

Before proceeding next please look at the http://www.roytuts.com/database-connection-example-in-php-and-mysql/

Create comments.php

Retrieve all the comments from database table.

Create helper.php

Below is the helper function which will help us to format the comments for display purpose

Create index.php

Code for comment page

Create comments_blog.js

Jquery and AJAX code while comment is posted by a user

Create add_comment.php

Add reply for a comment

Create comments.css

Add some styles to the comments

The following files are available at jQuery site. Please download these below files. You can download here also.

The final output

Thanks for your reading. Please leave a comment if you have any query.

 

 

 

23 Replies to “Nested comment system in PHP, AJAX”

  1. Hello i’m a novice in php ,please what should be the name of the data base you are using?thanks for your answers

  2. I think this code is not tested one. many places few thing is missing.
    e.g:
    Index .php:

    action file name is missing. it should be:

    In helper.php
    $reply_link = ‘%1$s%1$sreply%1$s’;

    can any one tell me from where $s is coming.
    why clicking on reply in not working???

  3. In helper.php
    $reply_link = ‘%1$s%1$sreply%1$s’;

    here what is $s , from where it is coming.
    why clicking reply is not working?
    please send solutions.

    1. you have not read the tutorial thoroughly, when I tried, I saw your page does not include jquery stuffs. I had already said in previous reply that you should read it carefully. Please make sure you have done everything before you post your comment. Have you downloaded the jquery attached files from the tutorial ?

      1. Great work Soumitra. it is working fine. I have done mistake and copied jquery in js folder. now I have moved everything in parent folder and working fine.

        I want to remove threaded reply so comment few line in healer.php, but still showing threaded:
        // if ($keep_track_depth <= 3) {
        // $reply_link = '%1$s%1$sreply%1$s’;
        // } else {
        $reply_link = ”;
        // }
        any specific changes is required?

  4. Hi,
    Thanks for this great tutorial!
    I would like to know how to add the href link of the user’s website on his nickname and to add a captcha system in French if possible?
    (Sorry for my English I am French)
    Thank you 🙂

  5. Hello there, Thank you so much for this tutorial. it worked wonders for me.
    the only question I have, or should I say the help I need, is how to change the date format that the script outputs?
    Currently the time stamp display (yyyy-mm-dd) I would like it to appear (dd-mm-yyyy)

      1. Thank you Soumitra….for your prompt reply……very kind of you.
        may I kindly point to you that I am just an amateur starting on codding.
        The question once again will be where would I put that line of code……I mean in which .php file and after which line of code?

        Sorry for inconvenience caused to you. and Merry Xmas.

        1. you see the lines $option[‘value’][‘comment_text’], // %4$s = comment
          ‘, ‘ . $option[‘value’][‘created_date’] // %5$s = comment created_date

          so replace $option[‘value’][‘created_date’] by date(‘d-m-Y’, strtotime($option[‘value’][‘created_date’]));

          1. HO ho ho…….Santa and Soumitra hava made my day!
            Thank you immensely Soumitra, the date displays as i wanted them to, and the whole thing works like a charm now,
            God bless.

  6. Hi ,

    I am facing problem while inserting values in database. I could submit the values from form without error , but values are not inserting into table.

    How to solve this ?

    What could possible went wrong ?

    FYI I am new to PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.