Advanced jQuery contact form with php support
In this modern era, most websites are using jQuery and ajax in their forms to enhance user experience.
In today’s tutorial, I am going to build an advanced php contact form with the help of Ajax and jQuery. This contact form can have the ability to send mail to user selected address from multiple email addresses. For example, if a business has multiple departments, then people can able to select a particular department they want to contact and the mail will go to the email address that corresponds to the selected department.
And interestingly, this contact form could work in both situations when javascript is enabled or disabled without any visual change between them except a page refresh.
Update(26 Dec 2011): Spam Protection added
Many people asking me how to add a spam protection for this contact form. In this update, I’ve included a simple math guard protection by Codegravity.com. Thanks for Codegravity
for the awesome spam protection code.
Download the ClassMathGuard.php class file from http://www.phpclasses.org/package/3926-PHP-CAPTCHA-validation-using-math-expressions.html and paste it inside the folder where you have the contact.php file.
Paste this php function just above the submit button code or paste it where you want the MathGuard question to show in the form.
<?php MathGuard::insertQuestion(); ?>
Also i’ve updated in the code $_REQUEST to $_POST for better protection.
I’ve updated my full php code below to include the MathGuard protection. Also you can download the updated source file below.
Basic HTML form
First, we will look in to building the html form. All the html elements should go in to contact.php
Contact Form
The first input of the form that I have included is a select box with 3 options namely sales, support and billing. Each option can have a unique email address which I will set it later in the php part, so the email will be sent to the email address corresponding to the selected option.
The remaining inputs are commonly used in most contact forms and the last one is a checkbox to let the visitor to have themselves a copy of the sending email.
There is one more input with type hidden. This input will determine whether or not the javascript is enabled in the client’s browser.
What is the actual plan?
We will set the hidden field’s value as “false” by default and alter it later with the use of javascript. So that way if the hidden field is unchanged, then we know that javascript is not enabled.
I forget to tell you about the div with ID “result” that you might noticed in the start of the form. I have used this div to print out all the php validation result.
Styling with CSS
Now we will write some css code to style up our form and save it as contact.css.
body {
background:#999;
}
#contactform {
width:400px;
margin:50px auto;
background:#fff;
padding:20px;
-moz-border-radius:15px;
-webkit-border-radius:15px;
border-radius:15px;
}
.form {
text-align:left;
font-size:18px;
font-weight:normal;
font-family:"Times New Roman", Times, serif;
color:#666;
}
h1 {
font-family: "Century Gothic", "Times New Roman", Times, serif;
font-size:28px;
color: #333;
margin-bottom:10px;
}
h1 span {
color: #00CC33;
}
input.text,textarea.text, select.text {
width:300px;
border:1px solid #CCCCCC;
padding:5px;
margin-bottom:10px;
background: url(input-bg.gif) top left no-repeat;
}
#result {
color:#FF0000;
font-style: italic;
margin-bottom: 5px
}
.message {
background: #33FF99;
padding: 5px 3px;
}
img.loading-img {
padding: 5px 3px;
}
textarea.text {
height:100px;
}
input#submit {
padding: 5px 10px;
background: #f2f2f2;
border:1px solid #e5e5e5;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
font-size: 18px;
color: #000;
-moz-border-radius:15px;
-webkit-border-radius:15px;
border-radius:15px;
}
Including css file and jQuery library in the document header
Include the css file that we just created and the jQuery library in to the document header.
Alter the hidden field’s value using javascript
The above simple code will set the hidden field’s(browser_check) value as true if the javascript is enabled. Otherwise, it remains unchanged.
jQuery for form processing
For the next part, we will play with some jQuery code to process the form submission.
We will begin with writing a click function to listen to the form submission.
$("#submit").click(function()
{
//code here
});
Now, inside the click function, we will add a bit of code to insert a fancy loader image. This is to intimate the user that the form validation is in progress. The following code will insert an image tag inside the div ID result.
$('#result').html('
').fadeIn();
The next thing I have to do is fetching the form data. To do this, I am going to use the jQuery function serialize(). What the function will do is that creates a text string in standard URL-encoded notation. The text string will look like
name=value&email=value&phno=value&subject=value&msg=value
I am initializing the text string in to the variable ‘input_data’.
var input_data = $('#form').serialize();
Next step is adding the Ajax.
$.ajax({
type: "POST",
url: "", //this will post the form values to the same page
data: input_data,
success: function(msg){
$('.loading-img').remove(); //Removing the loader image because the validation is finished
$('







Thanks mate for the great effort. I have one question though, how can we redirect the form page another page after submission?
I hope it is not hard because I was trying this out for few days and no luck so far. please let me know
Hi, Apologize the delay. As we are using ajax, we cannot use php redirect function. Instead you can use some like
if(msg == "Your mail has been sent succesfully!") {
window.location="http://tutorialstag.com";
}
No offence to the writer but i’ve already spotted like 2-3 typo’s and havent even finished reading lol — do they charge you for spellchecker in your time zone (just kidding) nice layout though I’ll give you that
Can I add multiple list/menu into this form?
Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a
lot of spam remarks? If so how do you prevent it, any plugin or anything you can suggest?
I get so much lately it’s driving me insane so any help is very much appreciated.
Today, I went to the beachfront with my kids. I found
a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her
ear and screamed. There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is
completely off topic but I had to tell someone!
Really Nice support!
You can have a look here to have also another point of view
http://mcwehbe.com/demo.php?imenu=4
This form is very workable for me, but I need the “post” form if someone has one.
Thanks in advance!
Very good. Thank for script