Since just about every website in the world has a “Contact Us” page, it is important for you to know how to create a simple contact form. Using a form instead of an email hyperlink is fairly easy.

Below is a simple form that will allow your visitor to send you a message through your web page:

contact-01

The code for this form is written using basic PHP:

<?php

if ( isset ($_POST['submit'])) {  // Check for each form value when the form is submitted:

$problem = FALSE; // no problems!


if ( empty ($_POST['firstName'])) {	// alert the user that they forgot to fill in "First Name"

$problem = TRUE;
print ("<p>You forgot to fill in your <b>"First Name"</b>.</p>");
}

if ( empty ($_POST['lastName']))  {	// alert the user that they forgot to fill in "Last Name"

$problem = TRUE;
print ("<p>You forgot to fill in your <b>"Last Name"</b>.</p>");
}

if ( empty ($_POST['company'])) {	// alert the user that they forgot to fill in "Company"

$problem = TRUE;
print ("<p>You forgot to fill in your <b>"Company</b>".</p>");
}

if ( empty ($_POST['email']))  {	// alert the user that they forgot to fill in "Email Address"

$problem = TRUE;
print ("<p>You forgot to fill in your <b>"Email Address"</b>.</p>");
}

if ( empty ($_POST['message']))  {	// alert the user that they forgot to fill in "Message"

$problem = TRUE;
print ("<p>You forgot to fill in your <b>"Message"</b>.</p>");
}

if (!$problem) { // if there are no problems:

print ("<p>Thank You for contacting Premium Design Works, <b>{$_POST['firstName']}</b>!</p>\n"); // Thank the user.

$mailMesage =  ("{$_POST['firstName']} {$_POST['lastName']} {$_POST['email']} from {$_POST['company']} wrote: \n\n{$_POST['message']}"); // message of the email.

$mailSubject = "{$_POST['subject']}"; // subject of the email.

mail ('msinkula@premiumdw.com', $mailSubject , $mailMesage); // Send the email.

}
}
// This is the ACTUAL FORM!

print '
<form name="contactForm" method="post" action="contact.php">
<h2>First Name:</h2>
<p><input name="firstName" type="text" id="firstName" size="30" value="' . $_POST['firstName'] . '" /></p>
<h2>Last Name:</h2>
<p><input name="lastName" type="text" id="lastName" size="30" value="' . $_POST['lastName'] . '" /></p>
<h2>Company:</h2>
<p><input name="company" type="text" id="company" size="30" value="' . $_POST['company'] . '" /></p>
<h2>Email:</h2>
<p><input name="email" type="text" id="email" size="30" value="' . $_POST['email'] . '" /></p>
<h2>Subject:</h2>
<p><select name="subject">
<option value="General Inquiry">General Inquiry</option>
<option value="Subject Two">Subject Two</option>
<option value="Subject Three">Subject Three</option>
</select></p>
<h2>Message:</h2>
<p><textarea name="message" cols="40" rows="15" value="' . $_POST['message'] . '"></textarea></p>
<p><input type="submit" name="submit" value="send" /></p>
</form>
';
?>

Note: copy & paste the script into your web page.

The result of this form is a tidy little email that will show up in your in-box:

contact-021

How This Script Works

Let’s first start out with where the script goes in your page.

This script is all contained within a PHP script tag that you can drop into any div tag in your page:

contact-03

Next, let’s break this down into components to show you how it works. There are two main components.

First, is the form itself:

picture-5

Second, is the script that handles it:

picture-6

The form is just like any other form written inside a form tag. However, instead of using a external form handler, this form uses the page itself to handle the form.

You will use a typical submit button to process the form:

picture-11

When the submit button is clicked it will call the action of the form.

This action then reloads the current page of “contact.php”:

picture-9

When the page is reloaded it will process the script with the posted data using an “if/else” statement:

picture-12

If there are no problems on the user’s part, the form will thank the user using the “print” function. It will also send an email off to the recipient using the “mail” function:

picture-13

Note: the form is actually written inside of the PHP script tags to make the form “sticky.”

If the user forgets to fill in any of the fields, the script will let them know while keeping the post data already created:

picture-31

This is done by creating a “post” value with the name of the input field:

picture-2

Customizing This Script

Now that you have seen how this script works, you will need to make some basic adjustments to it.

Inside of the “mail” function, you will need to change the recipient’s email to the proper address:

picture-14

You will also want to change the name in the “thank you” message:

picture-15

Most importantly, you will need to change the value of the action to your page’s name:

picture-17

Things to Know About this Script

There are a couple of unfortunate downfalls to the simplicity of this script:

  1. You may notice the you get emails from “Nobody” or from your ISP’s administrator.
  2. You may notice backslashes around quoted text and text with apostrophes.

But, at least you now have a start on using PHP to handle your contact forms.

 

5 Comments:

  1. Jingwen Yu says:

    So this contact form does not require a form handler? It sends the e-mail directly once user hit submit? What about blocking out bots from generating spams?

  2. Leslie Strom says:

    I get gibberish at the top of the form:
    http://www.civitalogue.com/contacts.shtml

    Any idea what’s wrong here?

  3. Bruce Decker says:

    I am using your .php mail form. I like everything about it except it leaves me with one stinking validation error. I recopied your code into a new blank .html file and named it .php and posted to my server. Same thing. I don’t know enough about .php to figure out what could be wrong. Any help would be appreciated.

    Here is my site’s page: http://www.brucedecker.net/bonniehull/contact.php

    I modified your form to my needs.

    Thank you. I know you are busy.

Leave a Comment