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

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.




Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

27 Thoughts on “Nested comment system in PHP, AJAX”

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

  • I think this code is not tested one. many places few thing is missing.
    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???

  • 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.

    • 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 ?

      • 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?

  • 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 🙂

  • 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)

      • 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.

        • 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’]));

          • 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.

  • 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

  • Hi.. actually no comments is displayed below the comment box.. and the comment i have posted is not stored in the database.. is there anything that i have done wrong .. ? I have created all files mentioned above.. including the js files.. but im not getting it.. please anyone give a reply .. i will be grateful to you..

  • This is a brilliant threaded comment system. I really liked you tutorial.
    I am new in this field. I tested it – and working absolutely fine.

    I have just one -WISH.

    How can I make the comments “show” in left side without giving a gap of around 50 px.
    It could be ( without keeping the left GAP ) B replys to A.
    How can I make the Commented Box Smaller with Reply below his/her comment ( removing it from right side )
    In this IMAGE url, I tried to explain my Wish :

    Can you please help me to do it ?

  • Warning: mysqli_connect(): (HY000/1049): Unknown database ‘cdcol’ in D:\XAMP\htdocs\new\database.php on line 3
    MySQL connect failed. Unknown database ‘cdcol’

    can u please tell me thi type of error how i resolve it
    cuz this type of data base is not use in any file ?

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.