[ Personal Stuff, Freebies, Blog, Portfolio, Store and Web Services ]

PHP and MySql Comment Form without Refreshing Page with jQuery

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

PHP and MySql Comment Form without Refreshing Page with jQuery

PHP and MySql Comment Form without Refreshing Page with jQuery

index.html

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 and result or response data is appended in 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.

ajax.php

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

Database

You can do this easy in PHPMyAdmin, or you can use this code to execute commands in MySql.

config.php

This contains PHP database configuration file.

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

Tagged with:

Related Articles

  • http://www.theictlegend.com Timothy

    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?
    but
    I love the simplicity of the code

    • http://www.sutanaryan.com/ Ryan S

      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.

      Thanks,
      Ryan S

  • http://www.raybanoutletshop.com AstegeAlopsk

    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.
    http://www.raybanoutletshop.com

  • http://www.raybanoutletshop.com AstegeAlopsk

    Includes a protective case and cleaning cloth.
    view here

  • http://www.koreanturk.com/kore-dizileri/fondant-garden fondant garden

    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.

    -Mike

  • http://www.smp1genteng.sch.id larjas

    nice script bro… must try this ;)

  • Peter

    let’s see if it works