html form guide
  about

Free, simple, PHP based email contact form

This page presents the sample code for a simple 'contact us' form. Such a form is usually present on the "contact us" page of websites. This form collects some information from the visitor and emails to the site admin, after validating the data. In this section we shall discuss, how to implement it.

The HTML contact form code

The very first step is the creation of HTML form that will collect the user information. For the sake of simplicity we are considering a simple contact form with only 4 fields. Let us consider that we want to collect the user information like name, email address, postal address and message. In this case the HTML required to create this form will be as follows:

<form method="POST" name="contact_form" action="contact-form-handler.php">
Enter Name: <input type="text" name="name">
Enter Email Address: <input type="text" name="email">
Enter Postal Address: <input type="text" name="postal_address">
Enter Message: <textarea name="message"></textarea>
<input type="submit" value="Submit">
</form>

By simply placing the above code in your HTML page, a simple contact form with 4 fields and a submit button will be created. You can name this page as "email-contact-form.html". When the user fills the form and hits the "Submit" button, user is taken to the PHP page called "contact-form-handler.php".

Validating the form submission

After entering the required information in the fields of HTML form on "email-contact-form.html" and hitting the "Submit" button, user will be taken on "contact-form-handler.php". This is the PHP page that will contain the code required to do the rest of the operation.

Before sending the form data to the admin of site, it is necessary to validate the data. For example, if a user simply hits the "Submit" button, without entering any information, empty email will be sent to the admin. Another example is the use of fake email address in the "email address" field.

We should place some PHP code in contact-form-handler.php that should validate the data. If the data is not valid, user should be redirected to "email-contact-form.html" again, so that he must enter the correct information. If the data is valid, then email should be sent to admin.

Below is the PHP code that will check the data of all fields of contact form. The email address field will be checked for the proper syntax. This code will also check if "name", "postal address" and "message" fields contain some data or not. In case of empty data or wrong email syntax, an error message will be displayed on the screen with a link back to "email-contact-form.html".

<?php
$error_occured = "no";
$name = $_POST['name']; // getting the value of name
$email_address = $_POST['email']; // getting the value of email address
$postal_address = $_POST['postal_address']; // getting the value of postal address
$message = $_POST['message']; // getting the value of user message

////////////////// Data Validation ////////////////
if(empty($name)) {
$error_occured = "yes";
echo "Error: You have NOT entered your name. Please click on BACK button of your browser and correct this error to proceed.";
}

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address)){
$error_occured = "yes";
echo "Error: You have NOT entered the correct email address. Please click on BACK button of your browser and correct this error to proceed.";
}
if(empty($postal_address)) {
$error_occured = "yes";
echo "Error: You have NOT entered your postal address. Please click on BACK button of your browser and correct this error to proceed.";
}

if(empty($message)) {
$error_occured = "yes";
echo "Error: You have NOT entered your message. Please click on BACK button of your browser and correct this error to proceed.";
}

?>

This code is getting the form values in PHP variables and validating them. The form values are extracted from the POST array, because we had set the "method=POST" while defining the form.

After getting the form values, we have defined a variable "$error_occured". The value of this variable is "no" in start. In case any error occurs, its value is changed to "yes". We shall check the value of this variable before sending the email. If the value is "yes", then email will not be sent and the user will be returned to previous page.

We have used the PHP's empty() function to check if the variable is empty. For validating the email address, regular expression(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$) is used. This expression is checking the syntax of the email address entered.

Emailing the form data using PHP

Now we shall use the above information to compose the email and send to the admin of the site.

<?php
if($error_occured=="no") {

$to = "AdminEmailAddress@example.com";
$email_from = $email_address;
$email_subject = "New Message For you";
$email_body = "You have received a new message. Here are the details: Name = " . $name . ", Postal Address = " . $postal_address . ", Message = ". $message;
$headers = "From: $email_from";

mail($to,$email_subject,$email_body,$headers);
}
?>

In the above code we have defined the variables that are required to send the email. This piece of code is starting with an "if" condition. Here we are checking the values of the variable, "$error_occured". If value is "no", then it means that there were no errors and the email will be sent. If the value is "yes", then it means that there were some errors. In this case, the email will not be sent and user will be simply asked to correct the errors.

"$to" is holding the email address at which the email will be sent.
"$email_from" is holding the email address of the user who has submitted the form.
"$email_subject" is holding the subject of the email that will be sent.
"$email_body" is holding the actual message that will sent in email. It is containing the name, postal address and actual message of user.
"$headers" is holding the information for use in the mail() function. This is holding the email address of sender.
The last line is the mail function that is using the above info and sending the email to the site admin.

Download the code for the contact form

Click here to download php-email-conact-form.zip

There are two files in the zip file. One is "email-contact-form.html". It is holding the simple HTML contact form with a submit button. The other page is "contact-form-handler.php". This is the PHP page that is getting the data, validating the data, reporting the errors (if any) and sending the email.

Why to have a contact form?

The contact form allows your web site visitor to send the email messages through the web interface. Your web site visitors could be asked for the views about the services or products offered. The visitors can also ask questions about the product or services. They can submit their suggestions too.

Contact forms are the easiest way of communication between the visitors and the webmaster. See also: Secure your HTML contact form using captcha

  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb


Related pages

 
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Email forms
  How to get email from html form submission

Email form using 'mailto'

Form to email using PHP

Email form with file attachment in PHP

Form mail scripts
  Form mail script selection guide

Perl based form mail

Contact Forms
  PHP based email contact form

Secure your HTML contact form using captcha
PHP Form
  PHP form validation script

PHP form tutorial: first steps

PHP form processing

PHP form 'GET'

PHP form 'POST'

Handling checkbox in PHP form processor

Handling select box in a PHP form

Using PHP_SELF in the action field of a form

How to submit a form using PHP

Creating a registration form using PHP

Making a login form using PHP

Creating a multi-page order form using PHP

Passing PHP form variables from one page to other

  Copyright © 2008 html-form-guide.com . All rights reserved.