This tutorial is about creating a simple contact form using XHTML CSS, PHP, and jQuery. The Aim of this article is to show some of the nice CSS 3 style properties such as adding Background Gradient, Corners and Borders to Form Elements.
Step 1 – The HTML Mark Up
The Step 1 is the backbone of the form. It Contains the HTML Code to add textboxes, textarea and a submit Button.
<form> <p> <input name="name" type="text" id="name"/> <label for="name">Name </label> </p> <p> <input name="name" type="text" id="email"/> <label for="email">Email </label> </p> <p> <input name="website" type="text" id="website"/> <label for="website">Name </label> </p> <p> <textarea name="text"></textarea> </p> <p> <input value="Send" type="submit" id="Send"/> </p> </form>
Step 1 - xHTML Form Backbone
Step 2 – Styling with CSS ( Without CSS3 Properties )
In this step, Basic CSS Style is added to make the form a bit more elegant .Pretty Simple Stuff here, nothing special. The wooden background texture used has been taken from flickR here .
body { padding:50px 100px; font:12px Verdana, Geneva, sans-serif;
background:url("wooden_background.jpg") repeat scroll 0 0 #282828;
}
input, textarea {
padding: 8px;
border: solid 1px #E5E5E5;
font: normal 12px Verdana, Tahoma, sans-serif;
width: 200px;
}
textarea {
width: 400px;
max-width: 400px;
height: 150px;
line-height: 150%;
}
.form label {
margin-left: 10px;
color: #999999;
}
.submit input {
width: auto;
padding: 9px 15px;
background: #617798;
font-size: 14px;
color: #FFFFFF;
cursor:pointer;
}
#form-div {
background-color:#F5F5F5;
padding:15px;
}
#wrapper {
margin:30px auto;
width:500px;
}
Step 2 : Basic CSS Style
Step 3 -Adding CSS 3 to beautify the Form
We now use some CSS3 Style properties to add some shadow effect , a gradient to the background of each form element and also rounded corners .The thing about Css 3 Property is because it is not the standard and officially yet , so we have to use browser-version CSS3 Property . So for Mozilla we use ,we use the ‘-moz’ prefix and for webkit browsers such as safari and chrome we use ‘-webkit’ .
- Add Shadow Effect
box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/* Default property recognized by some browsers- a Good practice to include it*/ -moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/*For Mozilla Firefox Browsers*/ -webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/*For Webkite browsers - Chrome and Safari*/
This CSS Property Takes a color and 3 Length as attribute .
Color - It can be in Hex/Rgb/Rgba
Horizontal offset-Positive Value means shadow will be on the right, and a negative value indicates shadow will be on the left.
Vertical Offset -Positive value means shadox will be on the top and a Negative value indicates shadow will be below .
Blur Radius - The higher the number , the more blurred it will be .
For this example , i have used RGBA .As compared to RGB , RGBA is simply color with Opacity .
Syntax for RGBA :
rgba[RED][GREEN][BLUE][ALPHA] /*Alpha = Opacity*/
- Add Gradient to Background
background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF));/*Chrome and Safari*/
background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 25px);/* Firefox Browsers */
background: #FFFFFF url('bg_form.png') left top repeat-x;
The 3rd one is not a CSS 3 Property .It’s purpose here is to mimic it in browsers like Internet Explorer 6,7,8 since it does not support CSS3 .
- Add Rounded Corners
-moz-border-radius:12px 12px 12px 12px; -webkit-border-radius: 12px 12px 12px 12px;
Unfortunately rounded corners will not be rendered in Internet Explorer .You can go to CurvyCorners to make it work on IE Browsers .
Step 3 : Form with CSS3 Properties
Step 4-Client Side Validation and Ajax Submission with jQuery
I used the Validation Engine Plug in to add Validation to the form. You can find more about this with. Cedric’s Post .
$(document).ready(function() {
// SUCCESS AJAX CALL, replace "success: false," by: success : function() { callSuccessFunction() },
$("#form1").validationEngine({
ajaxSubmit: true,
ajaxSubmitFile: "ajaxSubmit.php",
ajaxSubmitMessage: "Thank you, We will contact you soon !",
success : false,
failure : function() {}
})
});
Step 4 : jQuery Validation Added
Step 5- Sending Mail with PHP
The PHP Code below handles the Email Submission For the online Demo ,the Email Submission has been disabled to prevent abuses. You can download the source code using the download link below to get the full working code.
//Code Updated on 18 Feb 2011
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "ismaakeel@gmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, "From: {$email}");
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
}
Related Posts
- Designing a form using pure CSS enhanced with Jquery
- Username Availability Check in Registration Form using Jquery/PHP
- Creating a Fancy Search Feature with PHP,Mysql and Jquery
- Create a facebook post to wall system using PHP and jQuery Part 1
- Creating Message boxes using css with fadein Effects using jquery
{ 45 comments… read them below or add one }
I really like the looks of this form. I can’t get it to work though..I am not sure where to fill in the emailadress where I need it to send the message to. Also when I test in a browser it says
error in the ajax: 0 null.
Hope somebody here can help me on my way.
Thanks
This is what I’m looking for, very fancy one, thanks..
Awesome. I am using it on my website with a few modifications. Thanks very much.
this only good run on firefox 3, how trick if use ie, opera ?? thanks
Hello, very nice, but the email destination where it is?
Very very nice, but yes..where do you plug in the destination email. Thanks!
Hi, where is the E-mail destination? I tried to create my own variables, but it didn’t work;-
$to = “info@designsg.co.uk”;
$subject = “DesignSG Website Feedback”;
Very nice, thnak you
Hi, where is the E-mail destination?
A big thanks!!!
How can I implement ReCaptcha into this form? Thank you very much.
Sorry for the second question but how can I add a drop down list and have it validate? Can you implement check boxes as well?
Where do we put it our e-mail … some of us arent php or jquery GODS so we dont know all this… it would be nice if you showed this in your tutorial
I’d also like to know where to plug in the destination email address.
Any help would be greatly appreciated!
This has come in really handy, thanks.
Hyder Can you talk us overt the email sending part, your php script dosent seem to have anywhere to add an email address?
Could you tell us as this is suck a great contact form.
Thanks
Hello all ,
sorry for the delay . i have not blogged since months ! Code/Article Updated . You may re download using the Link :) ENjoy !
unfortunately the download link is broken…
Hi The download link aint working.
Thx
Download Link is working again . Let me know if you have any issue
Wonderfull tutorial .Really appreciate it.
Dear Hyder,
I’ve attempted to read your PHP and searched for notes within the updated ZIP file you’ve kindly allowed us all to download…I too have not been successful in discovering where to place “return email info” within the PHP (assuming that is where it goes).
Without the line spaces, I’ve included the PHP below. Could you suggest placement and form of return email info?
You have served a great need by sharing your expertise with us all and I humbly thank you for your fine example to all readers.
Best ever,
Frederick Park
Hi amazing tutorial thanks very much i love it!!
However i really need someone’s help.. …
I am really new to web design and i have added your cool contact form at the botom of my page which all looks fine. Except for some reason the red Speech Bubbles do not appear when no details are entered it just appears at the bottom in standard text. Also how do i add a phone number box as well? any ideas would be fantastic thanks in advance. I am very stuck!
here is the form../ http://www.martinmoores.com/Templates.html
Hi there Hyder
Thnx for the great Form … however i have a question ,
instead of people will see this :
” ajaxSubmitMessage: “Thank you, We will contact you soon !”, ”
I want to send them to another webpage
How can i do that ?
cheers m8
Hi great contact form. How do I change the colour of the thanks for emailing window. the yellow box that appears after sending?
Hi,
I just downloaded this form and change the email address in the php file to my own, but it did not work.
-In your article you stated: “For the online Demo ,the Email Submission has been disabled to prevent abuses. You can download the source code using the download link below to get the full working code.”
-Where can i find this and change to work local on my end?
thanks,
natasha
This is just not working, i exacly download and upload the contact form and when i click just nothing happen, anyone know why???
how to make a regex in Name and messages is a cyrillic?
@Marco : these 2 links will definitely help you to build the regex :
http://www.phpfreaks.com/forums/index.php?topic=327483.0
http://www.ibm.com/developerworks/library/os-php-unicode/index.html
thx :-)
I have downloaded this and test it’s working fine and I found this one is the best and fancy form validation script thanks for sharing….
Hi, thank you for your great work but is’s not working to me.
According to firebug there is an:
Uncaught SyntaxError: Unexpected token < jquery.validationEngine.js:413
Can you please help?
hi,
has anybody found where to put the email in so that when send gets pressed it sends to the email address, cant find anywhere in the php…
Hi
Thanks for fancy contact form but it is not working for me!
I am using wamp Server 2 with PHP 5.3.4 and I also changed the email address at “ajaxSubmitt.php” to $receiver = “suffii@gmail.com” ; but nothing happens whenever I click on send btn?!
To me , it seems the validation jquery works fine because I get the red boxex when the boxes are not fill properly but after that the button do nothing!
Can you please let me know what is wrong?
Same with me sufi. validation works fine but the send button just doesnt seem to do anything.
james,
there should be something wrong with wamp!
I tested the form with my web host and it works fine there without any problem!
Hi Sufi, I’m now getting an error that says “you are not going into the success fonction and jsonValidateReturn return nothing”
I’m not sure if it’s this line:
[CODE]
$(document).ready(function() {
// SUCCESS AJAX CALL, replace “success: false,” by: success : function() { callSuccessFunction() },
$(“#form1″).validationEngine({
ajaxSubmit: true,
ajaxSubmitFile: “ajaxSubmit.php”,
ajaxSubmitMessage: “Thank you, We will contact you soon !”,
success : function() { callSuccessFunction() },
failure : function() {}
})
});
[/CODE]
what do you have in the success: line?
Hi,
it’s very nice i like it and it’s working !
But
I need your help, i want to receive mails in arabic
it’s look like this:
عزيزي عضو كليبسر
i want to set the charset in utf-8
please help how to modify the code
A big thanks!!!
Great code and design, thanks for posting.
I noticed an error in the above code however, the form does not have an id and the jquery call is looking for a form with id form1. Anyone having trouble just edit the form tag to include id=”form1″
thanks for share and tutorial
Works great, thanks!
For those of you running into the issue of not knowing where to put your e-mail address replace the downloaded ajaxsubmit.php code with this:
The code from the downloaded source is missing the $receiver information. Put your e-mail address where it says PUT YOUR EMAIL HERE.
Also, when testing your form use a valid e-mail address as the testing e-mail because I noticed the form will not send anything if the e-mail address isn’t valid.. ex. test@test.com won’t work
Hope that helps!
sorry about that here’s the code:
<?php $name = $_POST['name']; // contain name of person $email = $_POST['email']; // Email address of sender $web = $_POST['web']; // Your website URL $body = $_POST['text']; // Your message $receiver = "PUT YOUR EMAIL HERE" ; // hardcord your email address here - This is the email address that all your feedbacks will be sent to if (!empty($name) & !empty($email) && !empty($body)) { $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}"; $send = mail($receiver, 'Contact Form Submission', $body, "From: {$email}"); if ($send) { echo 'true'; //if everything is ok,always return true , else ajax submission won't work } } ?>Another helpful hint: If you want to receive the users e-mail address on the submission e-mail you receive in your inbox you need to replace
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";with
$body = "Name:{$name}\n\nEmail:{$email}\n\nWebsite :{$web}\n\nComments:{$body}";This seems to be good. Going to try that for my website with few modifications in CSS.
Hi I have tried this on my website http://www.alexrostron.co.uk but I can’t get the email to submit :(
Do you need winamp on your host server and would the file have to be index.html or index.php?
thanks for any help