PHP and MySql Comment Form without Refreshing Page with jQuery

In this tutorial I’ll show you how to create an advance comment form in PHP and MySQL with jQuery without refreshing page in just a few lines of codes all we need to do is initialize the submitted post data and pass using jQuery API, display response and done, the data submitted is constructed using PHP, so sounds simple right? Yes it is.

Past few years Google introduces web 2.0 in their Gmail project with jQuery and now web programming is going crazy, adding and optimizing web using jQuery to save time, smooth and with elegant look.


To view demo page click here


Here our HTML codes that contains the basic input data name, email, url and message.

Javascript Code

$("#formcomment").submit() – formcomment is the ID of the FORM tag. If user submit the form, the data posted to ajax.php without refreshing the page(thanks to jQuery for the JS library) and result or response data is appended to commentpost class.

In here we’re using $.post() jquery API, it Load data from the server using a HTTP POST request, for more information about this API visit jQuery api site.


This contains PHP codes that construct the submitted data and MySQL code to insert data to tbl_comment table.


You can do this easily using PHPMyAdmin, or you can use the code below and execute commands in MySQL Console.


This contains PHP database configuration file.

That’s it, hope your find this tutorial helpful, for your feedback you can add comment below.

by Ryan Sutana

  • Tremendous things here. I’m very satisfied to see your post. Thanks so much and I’m taking a look forward to contact you. Will you kindly drop me a e-mail?

  • Pingback: Clockwork Spiral()

  • Matchless topic, it is very interesting to me )))) tHANKS tEAM

  • There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  • Good day very cool blog!! Guy .. Excellent .. Amazing .. I will bookmark your web site and take the feeds additionally?I am glad to seek out numerous useful info right here in the publish, we’d like develop more techniques in this regard, thank you for sharing. . . . . .

  • Woah this blog is great i love studying your articles. Keep up the good work! You know, a lot of persons are looking round for this info, you can aid them greatly.

  • The next time I read a blog, I hope that it doesnt disappoint me as significantly as this 1. I mean, I know it was my choice to read, but I truly thought youd have some thing fascinating to say. All I hear is a bunch of whining about something that you could fix in the event you werent too busy seeking for attention.

  • Good content, nevertheless, I had been questioning once you may generate a litte much more this approach subject matter? I might possibly be fairly glad if you may detailed a bit more. Cheers!

  • Can I just say, this blog is what got me by way of the day nowadays. Each time I read it, I just get far more and a lot more excited about whats next. Really refreshing blog and very refreshing ideas. Im glad that I came across this when I did. I enjoy what youve got to say as well as the way you say it.

  • Some really interesting details you have written on .Aided me a lot, just what I was searching for 😀 .

  • But i want to tell that this is extremely helpful, Thanks for taking your time to write this.

  • I get recently started a blog, the knowledge you prepare on this website has helped me greatly. Thanks for all of your cycle & work. “It is no exploit truism, ‘We are doing our pick.’ You get got to rise in doing what is vital.” by Sir Winston Churchill.

  • I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing ability has inspired me. Really the article is spreading its wings rapidly. =-= cialis kaufen apotheke

  • Good evening! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a wonderful job!

  • Wanting ahead to studying more. Nice weblog post.A lot thanks again. Need more.

  • Incredible! Thank you! My partner and i regularly required to compose in my internet site as well. Should i such as a element of your post so that you can my web-site?

  • Nowadays there is too a lot garabage written on blogs, glad to see your blog has kept it’s integrety. Thanks

  • If I may possibly –perhaps you need to think about adding a few images. I dont mean to disrespect what youve said; its extremely enlightening, indeed. Nonetheless, I feel would respond to it more positively if they could possibly be something tangible to your suggestions. Keep it up, but put a bit more into it next time.

  • I installed like you said above and the form shows up but when I hit submit nothing happens.

  • Comic Con really is Hanukah, Christmas, your birthday and Nerdvana for five straight days.

  • I just can’t get this code to work and i need it so urgent.
    please can you upload its zip along with the jquery?
    I love the simplicity of the code

    • Hi @Timothy the tutorial above is pretty straight forward and easy to follow, I’ve updated the HTML code maybe that cause in your side not to work.

      Please post your error if any, so that we can help you better.
      btw here’s the tip, use the code format below.

      PHP code
      HTML code
      jQuery code

      and make sure the ajax.php is present and with valid path.

      Ryan S

  • A string of smaller barrier islands, running roughly parallel to the highway and about 5 miles to the north, makes up the Gulf of Mexico coastline.

  • Includes a protective case and cleaning cloth.
    view here

  • Truly necessary publish admin great one particular i bookmarked your web site see you in future webpage put up.

  • Mike

    Hi! Thank you so much for this code, it was exactly what I was looking for.

    I had a little trouble making it work, but finally got it. There are a couple of things I had to change:

    -in ajax.php on line 10, the last variable in the conditional should be ‘$comment’, not ‘$message’

    -in ajax.php on line 15, the query tries to insert into a ‘date_posted’ field, which doesn’t exist when creating the table as described. You either need to delete the field and its value from the query, or add that field to the table

    -in config.php line 7 I think there is a problem with “die(mysql_error() ‘Oops! there is a problem connecting to database’)”. Is there supposed to be a concatenation in there? I deleted “mysql_error()” from that line and then it worked

    -in the jQuery script, the function needs to be wrapped in the document ready function

    – there is no element with the class “commentpost” for the jQuery to append the data returned by the ajax.php. A solution is to create an empty div with the appropriate class in the form page

    -you don’t explicitly show config.php being required. It may be obvious to people with more php experience, but people struggling with this might be helped by mentioning to require it once at the top of ajax.php

    Other than that, great stuff, it really helped me out.


  • nice script bro… must try this 😉

  • Peter

    let’s see if it works

  • Kristoff

    I appreciate this is an older post, but I found it very helpful, much appreciated. I was just wondering if you knew of any reason why radio buttons weren’t carrying their value through?


    • Just make sure you’ve added radion value like below sample, or if you’ve past variable from PHP make sure you echo the variable.


      $gender = array(‘Male’,’Female’);

      <input type="radio" name="gender" value="” />
      <input type="radio" name="gender" value="” />

      Hope that helps

      • Kristoff

        Will give it a go. Cheers!

  • anyvideo entertainme

    Hi Ryan you have a good blog here.I have request can you do one tutorial on uploading images with site and www_HOST_ROOT

    • What you mean www_HOST_ROOT?, also there are already lots of existing tutorial uploading images or even video

      • anyvideo entertainme

        Ryan I am building an app that uses the file system to upload images as opposed to using a database.Site root is where is where I have all the web scripts, WWW_HOST_ROOT should be the systems natural path.This is a challenge since am not using a web hosting company I am using xammp on my local machine.I take time as well to check on the above mentioned tutorials.

        • Yeah yeah I understand correctly you want your computer act like the server or infact the server, yeah it’s possible, we’ve done this before what you need is open your firewall so anyone can access your IP, just make sure it’s secure, upload path should be the same or use IP instead.

          • anyvideo entertainme

            I ditched that I decided two use database to store my images,its easier that way, not sure how robust that is.This may be a little bit
            of topic but let me know if you have a solution.
            I can get one drop down field from my database using php and mysql,how do I get all the fields in my form to be dropdowns using data from my database.