Get $500+ of the best After Effects files, video templates and music for only $20!
Learn how to AJAXify your Comment Forms
videos

Learn how to AJAXify Comment Forms

In this week’s video tutorial, I’ll teach you how to take a simple, boring contact form and add some animations and an AJAX post request to submit the form to your MySQL database asynchronously.

In addition, we’ll also implement a bit of server and client-side validation to better secure our data. When submitting to the database, we’ll also review the most secure methods, including the use of prepared statements, which will bind the necessary values to each query, rather than embed them.

Screencast

*Note – please forgive me, but I had a slight cold when recording this. I apologize if my words are a bit mumbled.

Other Viewing Options

Conclusion

Example

This screencast demonstrated just one way to animate your contact forms. Truthfully, it’s probably a bit too much for a regular site! :) But we’re here to have fun with it. If you decide to implement something like this into your own site, perhaps a more toned down version, please do leave a comment with a link to the page for our review!


Add Comment

Discussion 101 Comments

Comment Page 1 of 21 2
  1. Yosy says:

    First!!!

    Waiting for the FLV version

  2. Stoian Kirov says:

    oops :P
    I replied by mistake :)
    Sorry ;)

  3. thanks for tutorial its really wonderfull

  4. Anil Kiral says:

    This is an amazing tutorial. I will make some amazing stuff with this.
    Thanks very much..

  5. Robbert says:

    Great, thanks! Haven’t done the complete screencast yet – wouldn’t let this opportunity to be one of the first to comment pass by. ;-)
    Anyway, I’m still pretty new to Ajax, but will take some time to figure out how I can add this functionality into the WordPress theme I’m creating.

  6. reg4c says:

    Hey Jeff,

    I am watching this right now but the buffering is killing me and the player does not allow me to forward through to a part that was not download. Any way for a non-iTunes download link?

  7. esranull says:

    very nice thanks post

  8. Alex Högberg says:

    Good one Jeff!

    Loved the animations, and thanks for the loader page :)

  9. Gabriel says:

    its a good video ! but a little large :P

  10. Jeff says:

    Just what I was looking for.. ;-)

  11. ibrahim K says:

    very nice thank…

  12. Alex says:

    Thanks,
    this is very helpful

  13. Ali Baba says:

    Nice Tut. I just wondering when next Codeigniter tut will be available

  14. Imamu Hunter says:

    That’s an awsome tutorial.

    I can’t wait until someone does a tutorial on those sliding navigation bar. The ones that the highlight object follows whatever is hovered over and when you click it, it stays.

    I want to create a template with that in it.

    If you know of one send me a link to info@lavishimage.com.

    • Robert says:

      Imamu Hunter, that’s actually the lavalamp plugin. Please answer to this post if you would like me to write a tutorial here for it.

  15. Hannes says:

    Nice!

    But isn´t the isset($_POST[$key]) at 13:30 in line 7 somehow unneccesary because items only show up in the $_POST Array if they are set!?

  16. Great work Jeffrey, I’m glad PHP-Tips could be of some inspiration. The effect comes built in with the theme for the CMS (chyrp), so sadly, I can’t take credit. Way to take it to the next level! :)

  17. Colin says:

    Does anyone else have trouble with the video player? Every time I try and scrub forward the video just fades out and it shows the replay button. I have to watch the whole thing beginning to end.

  18. Dimby says:

    Hi,

    You should check if the POST value exists … otherwise it will give a warning ;)

    Solution:

    It will check if there is any POST value submitted with the key”field”. If it exists it will show the value of the POST array otherwise it will display “Your name here” in the textbox.

    $var1 ? $action2 : $action1;

    Is the same as:

    if( $var ){
    $action2;
    }else{
    $action1;
    }

    If you don’t get any warnings and you don’t using this method, or any similar method you probably turned of the php warnings. To turn these on check your MAMP/WAMP settings or use above the page.

    – Dimby

    • Dimby says:

      I watched the whole tutorial and you did turn on your warnings after all :P
      I learned some new things today, thanks!

      One small thing, when you check for an error in the userinput php you created an error variable. You can also check is the $post array had any value’s with the count function. count($post) must return 1 otherwise somethings wrong ;).

      – Dimby

  19. a77icu5 says:

    Thanks Mr. Jeffrey Way

    Note: we miss the code igniter tutorials from scratch xD .

  20. Jay says:

    Hi Jeff,

    Another great tutorial!
    You know that problem you were having with the “li” item withing the “ul#comments” when you slid it down it would jump, well you fixed it by defining the height in jQuery, but all you need to do is define a width for the “li” in the stylesheet and that resolves the problem.

  21. leo rapirap says:

    very informative video.

    i’ve been looking screencast in validating forms both client and server side
    and found it in this video.

    learned a lot!

    Thanks Jeff!

  22. Yoosuf says:

    Jeff thanks for the if(is_ajax){} trick, it was really useful

    Yoosuf
    http://eyoosuf.com

  23. Cerium says:

    Nice screencast Jeffrey, but I’ve a question, why don’t you use $(‘#addComment form’).submit(), instead of $(‘#submit’).click() ?

  24. EvilCerium says:

    Well NAH, what if you press enter to submit, #submit HASNT BEEN CLICKED. its COMMON SENSE to submit a form WHEN IT HAS BEEN SUBMITTED and not button click ……….

  25. Ionut Bucur says:

    Amazing tutorial, thanks Jeffrey!
    Good video, the code is easy readable on the full screen.

    In the file leaveComment.php (downloaded) you miss the else statement on line 24 – else { header(‘location….. } so it displays twice the page…

  26. Isn’t it a good idea to begin the javascript using $(document).ready() ?

    Another very cool tutorial as always! ;)

  27. Rata says:

    You may mumble your words as much as you like :) …
    this screencast is great!

    ThanX
    Kind regards
    Rata

  28. One small thing,

    Creating an animation after someone clicks the submit button isnt exactly helping the user with the “how long does this take” feedback.

    Although I think what you have there is really cool, I dont think its applicable in real world applications.

    For example, what if the server hangs and what you have there is the only thing users see. Those users could and might navigate off teh page without the post effecting the DB.

    Wouldn’t it be more practical to add this animation to actual loading sequence?

    Again, nothing wrong with this option… it looks great, but…. you know…

    Great screen cast, I loved it…

    P.S – One of your very old ASP.NET screen casts has it where you create a that shows exactly what I’m trying to say… That being more practical and sending the correct feedback to the user.

  29. AA says:

    Thanks Jeff for this nice tutorial.

    Please resume the CodeIgniter tutorials. Could you please use AJAX in your next CI tutorial.

    Thanks again.

  30. Hello says:

    If you get an error like this:

    Notice: Undefined index: comment in C:\wamp\www\comments\index.php on line 127 – 129

    Which is this line(s):
    <input type="text" id="name" name="name" value="” />
    <input type="text" id="email" name="email" value="” />

    (sorry for the code is messed out)

    Just use this fix: (Line 127)

    <input type="text" id="name" name="name" value="” />

    And this one: (line 128)

    <input type="text" id="email" name="email" value="” />

    Finally: (Line 129)

    Enjoy!

  31. bopjo says:

    Thanks for this great tutorial. I’ve always wondered how to do this and find it very impressive whenever I encounter it on web sites. This is a great thing to have in one’s skill set as a web designer/developer. I love how it combines so many different technologies. Very cutting edge. Thanks again.

  32. bopjo says:

    When I try this on my own local testing computer I get this kind of thing displayed in the form fields when the page loads:

    Notice: Undefined index: comment in C:\htdocs\Tutorials\ajaxComment\index.php on line 129

    If I fill out the fields anyway the info is inserted in the DB but the ajax reloading of the page hangs up and things get wacky. If I refresh the page, my post is displayed.

    Any thoughts or solutions would be appreciated.

  33. John says:

    This was VERY helpful. Many thanks!

  34. David Moreen says:

    I love that aJax, plus when you add animated gifs. Way to get my blood flowing.

  35. Roy Ho says:

    Very nice tutorial! Keep it up!

  36. Joshua Spillers says:

    Another great tutorial. Thanks, Jeffrey!

  37. Michael says:

    Hey very nice Tut! It will be very helpfull for me

  38. Wow! This is got to be the longest tutorial/screencast I’ve seen you do. Out of curiosity, is it? if not, which one?

    Thanks again Jeff, you covered a lot!

  39. Nice one Jeff, picked up a few gems in there.

    Like the php javascript test, didn’t know about that one, but it tied in beautifully with this project.

  40. Sean says:

    You are insanely good at this!!!

    I hope to have you skills one day. I have been watching a lot of tutorials on PHP etc but I don’t seem to be soaking it up very quickly!!!
    It’s frustrating!

    I will keep watching your videos though and hope it clicks one day!

    Thanks Jeff

  41. josh says:

    Noob question alert:

    How would I go about reversing the order, whereby the newest loads on top?

  42. helloworlder says:

    Thanks! Had absolutely no trouble understanding you. explained everything so well.

  43. WebQueen says:

    This is a great tutorial, thanks for sharing. I’m however getting an error when trying to post a comment. It reads “Column ‘name’ cannot be null”. I’m passing the first parameter into the prepare as NULL and the next three as ‘?’. Anyone have similar issue

  44. l4yn3 says:

    learn a lot.

  45. carlos says:

    learn a lot too, really

  46. TheBigBoss says:

    Example on how to integrate it into WP would be nice !
    But thanks for this tutorial

  47. Alan C says:

    What is he using for the Lorem autocomplete.. thing ? Is it textexpander?

Comment Page 1 of 21 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.