Post image for Create fancy contact form with CSS 3 and jQuery

Create fancy contact form with CSS 3 and jQuery

by Hyder on July 22, 2010

in Html & CSS, Jquery, PHP

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

  1. Designing a form using pure CSS enhanced with Jquery
  2. Username Availability Check in Registration Form using Jquery/PHP
  3. Creating a Fancy Search Feature with PHP,Mysql and Jquery
  4. Create a facebook post to wall system using PHP and jQuery Part 1
  5. Creating Message boxes using css with fadein Effects using jquery

{ 45 comments… read them below or add one }

Jan September 4, 2010 at 11:36 am

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

Reply

Syscl September 18, 2010 at 2:03 pm

This is what I’m looking for, very fancy one, thanks..

Reply

Isti September 25, 2010 at 9:15 pm

Awesome. I am using it on my website with a few modifications. Thanks very much.

Reply

lovethis September 29, 2010 at 1:08 am

this only good run on firefox 3, how trick if use ie, opera ?? thanks

Reply

mark October 2, 2010 at 12:41 am

Hello, very nice, but the email destination where it is?

Reply

Turkish October 16, 2010 at 2:23 am

Very very nice, but yes..where do you plug in the destination email. Thanks!

Reply

Shaun October 18, 2010 at 8:32 pm

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”;

Reply

La Boite Digitale November 5, 2010 at 1:39 pm

Very nice, thnak you

Reply

Dohn November 16, 2010 at 11:46 pm

Hi, where is the E-mail destination?

Reply

Artyom Shmatok November 24, 2010 at 1:36 am

A big thanks!!!

Reply

Jacob December 2, 2010 at 5:54 am

How can I implement ReCaptcha into this form? Thank you very much.

Reply

Jacob December 2, 2010 at 6:22 am

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?

Reply

Jacob December 19, 2010 at 12:50 am

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

Reply

mike sewell December 23, 2010 at 10:34 pm

I’d also like to know where to plug in the destination email address.
Any help would be greatly appreciated!

Reply

Phil January 29, 2011 at 3:31 pm

This has come in really handy, thanks.

Reply

Logicpro8_user February 15, 2011 at 1:46 pm

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

Reply

Hyder February 18, 2011 at 4:41 pm

Hello all ,
sorry for the delay . i have not blogged since months ! Code/Article Updated . You may re download using the Link :) ENjoy !

Reply

fab February 23, 2011 at 7:25 pm

unfortunately the download link is broken…

Reply

byenary March 3, 2011 at 9:52 pm

Hi The download link aint working.

Thx

Reply

Hyder March 4, 2011 at 3:22 pm

Download Link is working again . Let me know if you have any issue

Reply

Alek March 9, 2011 at 11:39 am

Wonderfull tutorial .Really appreciate it.

Reply

Frederick Park March 17, 2011 at 6:32 pm

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

Reply

Flat_eric March 22, 2011 at 11:20 am

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

Reply

Jeroen April 17, 2011 at 12:49 pm

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

Reply

Jason Colman April 24, 2011 at 7:46 pm

Hi great contact form. How do I change the colour of the thanks for emailing window. the yellow box that appears after sending?

Reply

Natasha May 18, 2011 at 11:40 pm

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

Reply

Luis June 14, 2011 at 6:57 pm

This is just not working, i exacly download and upload the contact form and when i click just nothing happen, anyone know why???

Reply

Marco July 2, 2011 at 6:01 pm

how to make a regex in Name and messages is a cyrillic?

Reply

Hyder July 3, 2011 at 10:49 am
Frederik July 26, 2011 at 10:20 am

thx :-)

Reply

free php scripts August 3, 2011 at 9:39 am

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….

Reply

George August 20, 2011 at 4:05 pm

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?

Reply

James August 25, 2011 at 4:05 pm

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…

Reply

Sufi August 25, 2011 at 9:21 pm

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?

Reply

James August 26, 2011 at 4:47 pm

Same with me sufi. validation works fine but the send button just doesnt seem to do anything.

Reply

Sufi August 27, 2011 at 11:35 pm

james,
there should be something wrong with wamp!
I tested the form with my web host and it works fine there without any problem!

Reply

James August 28, 2011 at 10:01 am

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?

Reply

Farid October 14, 2011 at 12:12 am

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!!!

Reply

Stephen November 1, 2011 at 11:01 pm

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″

Reply

qve November 10, 2011 at 10:52 pm

thanks for share and tutorial

Reply

Liz November 11, 2011 at 2:05 am

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!

Reply

Liz November 11, 2011 at 2:06 am

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
    }

}

?>

Reply

Liz November 11, 2011 at 2:14 am

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}";

Reply

Deepak Pathak January 13, 2012 at 12:55 pm

This seems to be good. Going to try that for my website with few modifications in CSS.

Reply

Alex January 14, 2012 at 8:17 pm

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

Reply

Leave a Comment

Previous post:

Next post: