Slick (and Easy) Ajax Contact Form With jQuery
jQuery is basically amazing. It's no-brainer syntax makes it incredibly easy to learn, and it's chaining cuts tons of would-be Javascript into one or two lines. As an example of this, let me show how you stupidly easy it is to make a really slick, totally useful Ajax contact form using jQuery.
Here's the demo. Another (more heavily styled) example of this be seen on the Capsize Designs "About" page.
Our goal:
- A plain old form contaning:
1. Text input for name
2. Text input for email
3. Textarea for message
4. Submit Button
- When submitted, the form's fields will be sent (via AJAX) to a separate page ('send.php')
- The page will attempt to send the email, and return a code back to the sending page
- If successful, a success message will fade in, and the form will reset. The page will NOT reload.
- If unsuccessful, an error message will fade in.
So let's get started. The HTML is pretty simple. We have a basic form, blah blah. The only curveball is that we're going to be adding three paragraphs before the form, one for each status message that could be output, and set them to display: none;. This way, we can selectively fade in the appropriate message after the form is submitted. Here's the code:
As you can see, the form submits itself to 'send.php' via the POST method. The next thing we have to do is create this 'send.php' page which will take the inputs, check them for validity, and then send them in an email to an address in the file.
-
<?php
-
-
function valid_email($str)
-
{
-
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
-
}
-
-
if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
-
{
-
$to = "EMAIL@EMAILADDRESS.COM";
-
$headers = 'From: '.$_POST['email'].''. "\r\n" .
-
'Reply-To: '.$_POST['email'].'' . "\r\n" .
-
$subject = "Contact Form";
-
-
{
-
}
-
else {
-
}
-
}
-
else {
-
}
-
?>
This is probably the heaviest part of the process, but luckily you can pretty much use this code as-is and just replace EMAIL@EMAILADDRESS.COM with your email. The only real important parts are the echo statements at the end. These return the status codes to tell us if the email was successfully sent, and if not, why not? In this case, a '1' means success, a '2' means server failure (HINT: You CANNOT run this on localhost, and doing so will give you this error) and '3' means invalid email address or no characters were entered in the comment box.
As it is, the code will work. The email should send. But we web designers are greedy folk, and we want something more. Let's throw in a little AJAX goodness with the help of our good friend jQuery. The jQuery Form Plugin is a little lifesaver that turns a regular form into an AJAX form with one line. We can then pass our 'send.php' page's status code back to this function as a parameter, and decide which error message to display based on that code. Here's the Javascript for all of this:
-
<script type="text/javascript" src="jquery-1.2.3.min.js"></script>
-
<script type="text/javascript" src="jquery.form.js"></script>
-
<script type="text/javascript">
-
$(document).ready(function(){
-
$('#myForm').ajaxForm(function(data) {
-
if (data==1){
-
$('#success').fadeIn("slow");
-
$('#myForm').resetForm();
-
}
-
else if (data==2){
-
$('#badserver').fadeIn("slow");
-
}
-
else if (data==3)
-
{
-
$('#bademail').fadeIn("slow");
-
}
-
});
-
});
-
</script>
If you aren't familiar with jQuery syntax, feel free to learn a little bit or just ignore it altogether. Basically, the $(document).ready(function(){ tells the page to execute all this stuff as soon as it's ready for it. The ajaxForm() function tells the page to stay right where it is when the form is submitted. All the rest of that simply fades in the appropriate error message.
And that's pretty much it. Told you it was easy. If you have any questions, feel free to add them as a comment. If you're lazy like me, you can just download the files here.











April 26th, 2008 at 6:01 pm
Hey great tutorial, little question I can’t seem to get the js working for my site.
http://www.nickd.co.nz
Any ideas?
Thanks
April 26th, 2008 at 11:21 pm
Sure thing, Nick. I have emailed you about it.
July 11th, 2008 at 10:34 am
I enjoyed your writing style and I’ve added you to my Reader. Keep these posts coming.
August 7th, 2008 at 8:12 pm
Hello,
Do you know how i could make this form still usable if the user has disabled JS (having a “message sent” instead of the “1″ would actually be enough!)
Thanks in advance!
September 5th, 2008 at 12:41 am
Hi there. Thanks for this. Simple and effective… however, incomplete. I’ve updated the PHP to something more like this:
I also updated the jQuery to handle things a bit more correctly… if you first messed up the email address, and then submitted the form correctly, it didn’t delete the earlier error message. I’ve also made it so that if a form is properly submitted the form itself then fades out:
Hope that’s helpful to some!
Cheers,
-Alister
—
Alister Cameron // Blogologist
http://www.alistercameron.com
Mob. 04 0404 5555
Fax 03 8610 0050
Skype: alistercameron
Yahoo: Alister_Cameron
Twitter: alicam
September 12th, 2008 at 12:16 pm
I love this script but I can’t seem to get mine working. Is there anything obvious I’m overlooking?? http://www.macksgrill.net/contact
I haven’t styled it or positioned it yet, just want to get it functioning. Any help would greatly appreciated. Thanks!
September 13th, 2008 at 2:58 pm
@sarah
I can’t even get the contact form to open to test it. Any thoughts?
P.S. Nice job theming drupal (a.k.a. taming the beast). It looks great.
September 21st, 2008 at 5:44 am
Yahooooo
October 25th, 2008 at 9:28 am
[…] Easy Ajax Contact Form (jQuery) - Demo […]
October 26th, 2008 at 2:34 am
thanks for the script and the corrections! it will be really usefull.
October 26th, 2008 at 10:03 am
[…] Easy Ajax Contact Form (jQuery)| Demo […]
October 28th, 2008 at 5:11 am
very thanks
October 31st, 2008 at 5:42 pm
Great info.Very good article.Bookmarked your blog plz keep it up
November 14th, 2008 at 2:14 pm
Love the contact form and will be using it on my website.
December 3rd, 2008 at 9:11 pm
Have been trawling tutorials and scripts for hours and this one just takes the biscuit!
Amazingly simple. Thank you so much
December 4th, 2008 at 7:48 pm
[…] Capsize Designs Ajax Contact Form - Combined with the jQuery form plugin, an amazingly simple Ajax method of sending contact form details without a page refresh. Every other method I tried for the websites contact form was so much more complicated or lacked accessibility […]
December 27th, 2008 at 8:24 am
[…] jQuery Contact form - an extremely simple jQury+AJAX powered contact form provided by Capsize designs. […]