SEARCH
Newsletter
Subscribe to get design
tips, latest trends, free
stuff and more.
It doesn't look like an e-mail address

hosting

  Web Programming PHP In Depth Contact Form Part 2 - The PHP Code

In Depth Contact Form Part 2 - The PHP Code

Stefashwell.com PHP May 30, 2007

This tutorial follows on from "In Depth Contact Form Part 1 - The Form Itself" that describes the process involved in making the contact form as the user sees it. The second part of the tutorial details the PHP code that checks data has been entered and send an email containing this data.

OK, let's start.

The following will get the value entered into the "name" box on the contact form as the form uses method="post" we use $_POST['name'] to get the value the code puts it into a variable called 'name' for use later

$name = $_POST['name'];

The following "if" statement checks if there is a value in the variable $name, if not it means nothing was entered into the box on the form, and so it will echo out an error message and stop.

This isn't really necessary, and on some things you wont want to check.

if ( empty($name) ) {
echo 'You did not enter your name, please <a href="/img_articles/12085/contact.html">go back</a> and make sure you enter it';
die();
}

Now lets capture the rest of the form elements and provide error checking for each in the same way as above:

$company = $_POST['company'];

if ( empty($company) ) {
echo 'You did not enter your company, please <a href="/img_articles/12085/contact.html">go back</a> and make sure you enter it';
die();
}

$email = $_POST['email'];

if ( empty($email) ) {
echo 'You did not enter your email address, please <a href="/img_articles/12085/contact.html">go back</a> and make sure you enter it';
die();
}

$tel = $_POST['tel']; // for tel and fax it might not be necessary for it to be entered and so can be left blank
$fax = $_POST['fax'];

$subject = $_POST['subject'];

if ( empty($subject) ) {
echo 'You did not enter a subject, please <a href="/img_articles/12085/contact.html">go back</a> and make sure you enter it';
die();
}

$comments = $_POST['comments'];

if ( empty($comments) ) {
echo 'You did not enter any comments, please <a href="/img_articles/12085/contact.html">go back</a> and make sure you enter it';
die();
}

Ok now the boring bit is over we can start putting together the email message that will be sent ready for sending.

The following code creates a variable called $message and all of the variables from the form are added, along with extra text to go in the mail. carriage returns are created with "n".

$message = 'Someone has filled out the contact form on your website' . "nn";
$message .= 'Name: ' . $name . "n";
$message .= 'Company: ' . $company . "n";
$message .= 'Email: ' . $email . "n";
$message .= 'Tel: ' . $tel . "n";
$message .= 'Fax: ' . $fax . "nn";
$message .= 'Comments: ' . "n";
$message .= $comments;

In the code below 2 variables are set, one is the email address the mail will be sent to, and the other who to CC.

$sendTo = 'example@example.com';
$Bcc = 'example@example.com';

The code below sets message headers - from, reply to and bcc addresses.

$headers = 'From: Contact Form <' . $email . '>' . "rn" . 'Reply-To: ' . $email . "rn" . 'Bcc: ' . $Bcc . "rn";

Ok now we can send the mail! which is really simple in php:

mail($sendTo, $subject, $message, $headers);

Now the mail has been sent we can forward the browser to a confirmation page letting the user know their mail has been sent.

header ("Location: contact_done.html");

And that's about it! All that is left to do is create the HTML file called contact_done.html.

Downloads

Download the html and php files for this tutorial here.

   
subscribe to newsletter