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
{ 53 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 !
Hi Hyder, how are you doing? I have a question and hoping you could help me. I followed a tutorial about registration and activation email however after clicking the registration button, i did not receive any email from yahoomail… I tried to register using a webmail email and i received it. What could be the problem? I’ve checked on my spam and inbox in yahoo however it doesnt work for yahoomail or even gmail. here’s my code:
<?
include 'db.php';
// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info'];
/* Let's strip some slashes in case the user entered
any escaped characters. */
$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);
/* Do some error checking on the form posted fields */
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
echo 'You did not submit the following required information! ‘;
if(!$first_name){
echo “First Name is a required field. Please enter it below.”;
}
if(!$last_name){
echo “Last Name is a required field. Please enter it below.”;
}
if(!$email_address){
echo “Email Address is a required field. Please enter it below.”;
}
if(!$username){
echo “Desired Username is a required field. Please enter it below.”;
}
include ‘join_form.html’; // Show the form again!
/* End the error checking and if everything is ok, we’ll move on to
creating the user account */
exit(); // if the error checking has failed, we’ll exit the script!
}
/* Let’s do some checking and ensure that the user’s email address or username
does not exist in the database */
$sql_email_check = mysql_query(“SELECT email_address FROM users WHERE email_address=’$email_address’”);
$sql_username_check = mysql_query(“SELECT username FROM users WHERE username=’$username’”);
$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)){
echo “Please fix the following errors: “;
if($email_check > 0){
echo “Your email address has already been used by another member in our database. Please submit a different Email address!”;
unset($email_address);
}
if($username_check > 0){
echo “The username you have selected has already been used by another member in our database. Please choose a different Username!”;
unset($username);
}
include ‘join_form.html’; // Show the form again!
exit(); // exit the script so that we do not create this account!
}
/* Everything has passed both error checks that we have done.
It’s time to create the account! */
/* Random Password generator.
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php
We’ll generate a random password for the
user and encrypt it, email it and then enter it into the db.
*/
function makeRandomPassword() {
$salt = “abchefghjkmnpqrstuvwxyz0123456789″;
srand((double)microtime()*1000000);
$i == 0;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random_password = makeRandomPassword();
$db_password = md5($random_password);
// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date)
VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error());
if(!$sql){
echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
$userid = mysql_insert_id();
// Let's mail the user!
$subject = "Welcome to my Website Cebu! Member details";
$message = "Dear $first_name $last_name,
Thank you for registering at our website, http://www.greenbarley.com.ph!
You are two steps away from logging in and accessing our exclusive members area.
To activate your membership, please click here: http://www.greenbarley.com.ph/activate.php?id=$userid&code=$db_password
Once you activate your memebership, you will be able to login with the following information:
Username: $username
Password: $random_password
Thanks!
Green Barley Team
This is an automated response, please do not reply!";
mail($email_address, $subject, $message, "From: MyDomain Webmaster\nX-Mailer: PHP/” . phpversion());
echo ‘Your membership information has been mailed to your email address! Please check it and follow the directions! Thank you’;
}
?>
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}";It’s good form.
Can you help me, I get error data.status = 0 transport = error … I do not understand how to fix it.
YOU CAN Please tell me I’m doing wrong?
This gives me when trying to send the form … send the mail but not send data and shows me this error.
Of course, thank you very much!
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
Works perfect!!! Txs
Still the code below seems not working since i did not receive any message from my email where the message should supposed to be sent.
I have used fancybox to open the contact form inline. Why does not it works? Everything seems normal but the button is disabled.
Also do we need an action on the form? It is obligatory in XTHML 1.1 so sites in this doctype are not validates successfully.
anybody come up with a solution.. the submit button doesn’t work for me..
Please, some1 found solution for not working ajax submission?
When i click send – mail has been send but after this nothing happened, i mean the next part with “Thank you, We will contact you soon !” doesnt appear.
Cheers