Create an Awesome Contact Form – Part 3

Create an Awesome Contact Form – Part 3

By Jack Franklin on September 11th, 2009 in Tutorials

In Part 3 of this 3 part series, you'll learn how to add javascript and ajax to make the contact form submit without the need to refresh the page!

If you haven’t completed the first two parts to this series, visit Part 1 here and Part 2 here.

So, I was going to leave this series after Part 2 as we’ve done everything required of us. We now have a working form, we are protected against the spammers, and it looks pretty decent as well. But we have one more stage to go. This is entirely optional, but we are going to look at using jQuery and AJAX to submit the form without refreshing the page. Scary stuff, or so it sounds. But it’s pretty easy really. The beauty of this is that if the user does not have Javascript enabled, the form will work like it should. So you don’t have to worry about users not being able to use the form.

Step 1

First you need to include jQuery in your code. The jQuery source is actually hosted on the Google CDN, so we will include it from there to save some of our server doing the work. Add this line into your head:

<script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;></script>

And now lets start off by adding another script tag and the code that makes sure jQuery only runs when the dom is ready:

<script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;>
$(function() {

});
</script>

Step 2: The Hook

If you remember, I gave a unique id to the paragraph that contains the submit button on our form. We are going to hook into that using jQuery:

$('#submit input').click(function() {

})

Step 3: The AJAX

So, it’s time. We’ve done everything we need to do, and now we use jQuery’s AJAX function to bring it all together. I’ll show you all the code, and then talk through it:

$('#submit input').click(function() {

dataString = $('form').serialize();
$.ajax({
type: &quot;POST&quot;,
url: &quot;example.php&quot;,
data: dataString,
success: function() {

$('#message').empty().html(&quot;<h2>Contact Form Submitted!</h2>&quot;);

}

});
return false;
});

Woah, that seems like a lot, right? On further inspection it’s pretty easy. The serialize() function collects all our form values together, ready to send to the PHP code. That’s all it does, and it’s very useful. Next, once we start our Ajax function, we need to define a few things. The type is POST, as we discussed in P1 of this series. The url is going to be the same page that your form is on, as we included all our PHP code in there. For me, it’s example.php. The data, what will be sent, we pass in the variable dataString, which is where we saved all our data earlier with the .serialize() function. Finally we create a function called success. This will only be called if everything goes to plan. All we do here is select our div with an id of message, empty it out, and then add a short message to let the user know. After all of that, we use the line return false. This cancels the button’s default action, which in this case is to send the data and reload the page. By canceling this and using Ajax we can do all the process without a refresh. How fancy is that?!

The beauty is that it will still use all our PHP validation code and if anything goes wrong it will still echo out the errors which we coded in Part 1. And, if you try using the form with Javascript disabled, it will act like it did after we finished Part 2.

Please make sure that you added the div with the id of message yourself, as we didn’t add that before this lesson. All I did was simply add the div so it surrounded the PHP code for displaying our errors.

All Done!

So that’s it for this 3 Part series. We’ve done a lot! We’ve looked at how to process and validate a form, including spam protection. We then styled it with basic CSS, and now we’ve “AJAXed” it with some simple jQuery. I hope you’ve enjoyed the 3 parts, if you have any feedback I’d love to hear it so please do leave a comment. As always if you have any questions or cannot get the code working, leave a comment as well and I’ll be happy to help out.

Click here to download files for the entire 3 part tutorial.

Did you like it? Share it!
  • Delicious
  • DesignBump
  • DesignFloat
  • Digg
  • Facebook
  • StumbleUpon
  • Technorati
  • Twitter
About The Author
Jack Franklin

Jack is a 18 year old web designer and developer who lives in the beautiful area of Cornwall, England. Off the computer, Jack enjoys playing snooker, tennis, football and badminton.

Comments
  • September 11th, 2009 at 1:43 pm.

    Demo link?

  • September 14th, 2009 at 4:48 pm.

    i would also like a demo link please?

  • plajsa
    September 18th, 2009 at 5:00 am.

    lool… where is the demo-link?

  • September 28th, 2009 at 9:57 pm.

    I would like to know if this script will display as html or just plain text when reading the email from the form?

  • October 2nd, 2009 at 4:39 am.

    In the last line of this tutorial you have link to example file. Just download example.zip unzip and copy to server. Next change line 27 ( $sendto = “your_own_mail@domen.com”;)in example.php. Touch off this file in you’re web browser and check how it works.

    This script send plain text mail. So I don’t have idea what for is that structure with HTML tags ??? But nevermind…

    I found a couple bugs:

    First of all. That form is NOT sending when i pressed Send Button. After I’ll remove css style from this button (and it looks normal) button sending form.

    In IE i have problem with javascript. It says console undefinied on line 19.

    Do you have any idea what is the problem?

    Regards

  • lazy2k
    October 8th, 2009 at 1:51 pm.

    The zip file I download does not appear to be a valid archive

  • October 9th, 2009 at 7:58 am.
  • October 9th, 2009 at 8:18 am.

    Hey i have also downloaded this ans file is not CORRECT when unzipped. and i have downloaded the link Adam posted.

    when you unzip it has a file init called “example” and type of file says “FILE”

    which to me its not ziupped correctly

    Regards,
    Aaron

  • darren
    October 13th, 2009 at 6:30 am.

    hi jack
    nice form processor, i notice the form doesnt hold the values if it errors. i have tried to place as the value for name input. but this doesnt work. any reason for this? any sugesstions
    thanks

  • October 18th, 2009 at 10:29 am.

    Downloaded fine for me, will be adding to one of my sites today and will let you know how I make out.

    Thanks.

  • tmx
    February 21st, 2010 at 2:39 pm.

    Lots of problems with this form. Does not hold fields as noted above. Works fine with js disabled but validation breaks with js enabled (can submit blank form). JS errors…. a real mess. Thanks anyway.

Trackbacks

Toggle Trackbacks

  1. [... If you haven't completed the first two parts to this series, visit Part 1 here and Part 2 here. So, I was going to leave this series after Part 2 as ...]

  2. [... If you haven't completed the first two parts to this series, visit Part 1 here and Part 2 here. So, I was going to leave this series after Part 2 as ...]

So, what do you think?

x