Simple PHP Contact Forms

If you ever wanted to have simple contact forms for your website, you have come to the right place. This very easy to use script will e-mail you the results of the contact form. It is very flexible, and simple to insert on your website.

Overview of Method & Requirements

For this script to work correctly, your host must allow you to use PHP on your website, and the file for the script needs to end in .php

A neat feature about this form, is that an e-mail will only be sent when the information on the form is correct, if it is not correct, an error will be displayed and an e-mail will not be sent.

Explanation of Script

At the bottom you will be able to see the full script so you can understand the structure.

The beginning of the script, we declare values, these are the values that are on the contact form (First name, Last name, E-mail Address, Phone number, Website, etc.)

Each input box must include the name attribute.

This is correct: <input type="text" name="FirstName" size="20" />

This is incorrect: <input type="text" size="20" />

The name attribute for this particular text box is named FirstName. Earlier I mentioned the script will declare values, it will take the value from the text box FirstName, and assign it a certain variable.

For example, the text box with the name FirstName, would be declared like this: $contact_firstname = $_POST['FirstName'];

$contact_firstname is the name of the variable, and FirstName was the name of the text box on your form. If you entered Billy for your First name, $contact_firstname would be equal to Billy.

We use the same exact logic to add the other text boxes, such as Last name, E-mail address, etc.

The e-mail address that the form is sent to, is declared on the line that reads: $to = 'user@mydomain.com';

Likewise, the subject is declared on this line: $subject = "Message submitted using Contact Us form";

All of the above values including the $message can be configured to whatever value you wish to use. The message is on the line below the to address, and that line reads:

$message = "You have received a contact message:\r\n"
."----------------------------------------------------------------\r\n"
."Contact Name: $contact_firstname\r\n"
."Subject: $contact_subject\r\n"
."Submitted: $mydate\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contact_message\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";

Be sure to use the variables from the first part, notice the Contact Name: $contact_firstname is where the first name is display.

The IP address can be found by using {$_SERVER['REMOTE_ADDR']} and the address of the form can be displayed using: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}

You may add or delete lines on the form, but make sure that the last part displayed contains a trailing semi-colon ;

Example Script

Here is a complete example of the script that you can simple copy and paste on your website.

<html>
<head>
<title>Contact Us Form</title>
</head>

<body>

<?php
// declare values
$contact_email = $_POST['EmailAddress'];
$contact_subject = $_POST['Subject'];
$contact_name = $_POST['FullName'];
$contact_message = $_POST['Message'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );

// where to send e-mail to
$to = 'user@mydomain.com';

// e-mail subject
$subject = "Message submitted using Contact Us form";

// e-mail message
$message = "You have received a contact message:\r\n"

."----------------------------------------------------------------\r\n"
."Contact Name: $contact_name\r\n"
."Subject: $contact_subject\r\n"
."Submitted: $mydate\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contact_message\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";

$headers = "From: $contact_name <$contact_email>\n"
."Reply-To: $contact_email\n"
."X-Mailer: PHP/".phpversion();

// check for validation, then send the e-mail
if(empty($contact_name) || empty($contact_email) || empty($contact_subject) || empty($contact_message)) {
echo '<p>Send us a message, enter your information below and click \'Submit\'!</p>
<form method="post" action="">
<table id="Form-Details">
<tbody>
<tr><td>Name:</td><td><input type="text" name="FullName" size="20" /></td>
<td>Subject:</td><td><input type="text" name="Subject" size="20" /></td></tr>
<tr><td>Email:</td><td colspan="3"><input type="text" name="EmailAddress" size="20" /></td></tr>
<tr><td colspan="4">Message:</td></tr>
<tr><td colspan="4"><textarea rows="6" name="Message" cols="47" class="input"></textarea></td></tr>
<tr><td colspan="4" class="right1"><input type="submit" value="Submit" /><input type="reset" value="Reset" /></td></tr>
</tbody>
</table>
</form>';
} elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $contact_email)) {
echo "<p>ERROR: Please enter a valid e-mail address.</p>";
} else {
mail( $to, $subject, $message, $headers );
echo "<h3>Message Sent!</h3><p>Dear $contact_name,<br /><br />We will get back to you as soon as possible using $contact_email.";
}
?>
</body>
</html>

Copyright © Mr. Bob's Web Design, 2005. All Rights Reserved. Use of website and services are subject to the Terms & Conditions