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

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

13 thoughts on “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.
    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?

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.