Need a contact form for your site? This tutorial shows you how to create one.
This tutorial assumes you have a basic knowledge of PHP there are other tutorials on this site that explains some of the mechanics used here.
The contact form will be sent to your email address once the form is completed.
First we check that the form has been submitted:
<?php
//This code runs if the form has been submitted
if (array_key_exists('submit', $_POST))
{
?>
If the form has been submitted we check to make sure the fields are not empty and trim any white space.
We first check the name field We make sure the input is less then 3 but not more then 20 char actors using the function strlen (string length) if the char actors are less or more then specified then an error is set but its not printed out just yet.
<?php
// check fields are not empty
$name = trim($_POST['name']);
if (strlen($name) < 3) {
$error['name'] = 'Name Must be more then 3 charactors.';
}
if (strlen($name) > 20) {
$error['name'] = 'Name Must be less then 20 charactors.';
}
?>
Then we check for a valid email address: At the very least the email must have a character at @ symbol and another character
<?php
// check for valid email address
$email = $_POST['email'];
$pattern = '/^[^@]+@[^srn'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
$error[] = 'Please enter a valid email address';
}
?>
We then check the message input section to make sure its been filled in the same way we checked the name field
<?php
// check fields are not empty
$message = trim($_POST['message']);
if (strlen($message) < 3) {
$error['message'] = 'Message Must be more then 3 characters.';
}
?>
Now we make sure there’s no errors set and proceed if there are no errors if there is errors they will be printed above the form
<?php
// if validation is okay then carry on
if (!$error ) {
?>
If no errors set send the email. Change the email address to your email address you want to use to get the form details.
For the subject field I’ve typed in Contact for “.$_SERVER[‘HTTP_HOST’].” This will get the server address its come from like: http://www.example.com
The body is the form details this can be laid out any way you like to put text on a new line use (new line)
Addtionalheaders will include your email address so when you receive the email it will display your email address in the from field.
Lastly we check to see if the email has been sent using an if statement and if the email has been sent then we set the variable $sent to true.
We then close the brackets for validation and for submitted.
[code]<?php
//send email
$to = "[email protected]";
$subject = “Contact for “.$_SERVER[‘HTTP_HOST’].””;
$body = “on “.date(‘M j, Y’).” Information from contact form: Name: $name With the email address of $email Wrote this message: $message”;
$additionalheaders = "From: [email protected] ";
$additionalheaders .= “Replt-To: [email protected]”;
if(mail($to, $subject, $body, $additionalheaders))
{
$sent = true;
}
} // end valadation
}// end submit
?>[/code]
Once the email has been sent show a success message
[code]<?php
// If mail is sent
if (isset($sent)) {
echo "
Thank you
";echo "
We will be in touch shortly
";} else { // mail not sent show form
?>
[/code]
if the email has not been sent and errors are set show the errors then display the form
[code]<?php
echo “
Use this contact form for general enquires
”;echo "
I hate spam and I’m sure you do too, so don’t worry your email address will NOT be passed on to any third party’s
";// show any errors if set
echo “
”;”;
if (isset($error[‘name’])) {
echo “”.$error[‘name’]."
“;
}
if (isset($error[‘email’])) {
echo “”.$error[‘email’].”
“;
}
if (isset($error[‘message’])) {
echo “”.$error[‘message’].”
";
}
echo “
?>[/code]
Now we show the for. The form will reload the same page once its sent using
action="<?php echo $_SERVER['PHP_SELF']; ?>"
I’ve always set the form to be a sticky form so it there is an error the form keeps the inputted date using the value in the input field
<?php if(isset($error)) {echo "value='$name'";} ?>
After the form is finished we close the else statement that is still open
[code]<?php } ?>
<?php ContactName: />
Email: />
Message: <?php if(isset($error)) {echo $message;} ?>
<?php } ?>[/code]here is the full code:
[code]<?php
//This code runs if the form has been submitted
if (array_key_exists(‘submit’, $_POST))
{
// check fields are not empty
$name = trim($_POST[‘name’]);
if (strlen($name) < 3) {
$error[‘name’] = ‘Name Must be more then 3 characters.’;
}
if (strlen($name) > 20) {
$error[‘name’] = ‘Name Must be less then 20 characters.’;
}
// check for valid email address
$email = $_POST[‘email’];
$pattern = ‘/^[^@]+@[^srn’";,@%]+$/’;
if (!preg_match($pattern, trim($email))) {
$error[] = ‘Please enter a valid email address’;
}
// check fields are not empty
$message = trim($_POST[‘message’]);
if (strlen($message) < 3) {
$error[‘message’] = ‘Message Must be more then 3 characters.’;
}
// if validation is okay then carry on
if (!$error ) {
//send email
$to = "[email protected]";
$subject = “Contact for “.$_SERVER[‘HTTP_HOST’].””;
$body = “on “.date(‘M j, Y’).” Information from contact form: Name: $name With the email address of $email Wrote this message: $message”;
$additionalheaders = "From: [email protected] ";
$additionalheaders .= “Replt-To: [email protected]”;
if(mail($to, $subject, $body, $additionalheaders))
{
$sent = true;
}
} // end validation
}// end submit
// If mail is sent
if (isset($sent)) {
echo "
Thank you
";echo "
We will be in touch shortly
";} else { // mail not sent show form
echo “
Use this contact form for general enquires
”;echo "
I hate spam and I’m sure you do too, so don’t worry your email address will NOT be passed on to any third party’s
";// show any errors if set
echo “
”;”;
if (isset($error[‘name’])) {
echo “”.$error[‘name’]."
“;
}
if (isset($error[‘email’])) {
echo “”.$error[‘email’].”
“;
}
if (isset($error[‘message’])) {
echo “”.$error[‘message’].”
";
}
echo “
?> Contact
Name: />
Email: />
Message: <?php if(isset($error)) {echo $message;} ?>
<?php } ?>[/code]