PHP is a great scripting language that allows many dynamic functions in your site. You can create custom contact forms, form validation, and email responses using PHP. This article will explain the basics in creating an email form that validates the inputs, produces errors when inputs are typed incorrectly, and send an email to you when submitted.
This section of the code will validate the form inputs
Below is the code you will use to validate whether the inputs have valid data or not. This can be customized for different form field validations.
Note! You can paste the entire code directly in the body section of your webpage to get it working.
<?php
if (isset($_REQUEST['submitted'])) {
// Initialize error array.
$errors = array();
// Check for a proper First name
if (!empty($_REQUEST['firstname'])) {
$firstname = $_REQUEST['firstname'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}
else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
} else {$errors[] = 'You forgot to enter your First Name.';}
// Check for a proper Last name
if (!empty($_REQUEST['lastname'])) {
$lastname = $_REQUEST['lastname'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}
else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
} else {$errors[] = 'You forgot to enter your Last Name.';}
//Check for a valid phone number
if (!empty($_REQUEST['phone'])) {
$phone = $_REQUEST['phone'];
$pattern = "/^[0-9\_]{7,20}/";
if (preg_match($pattern,$phone)){ $phone = $_REQUEST['phone'];}
else{ $errors[] = 'Your Phone number can only be numbers.';}
} else {$errors[] = 'You forgot to enter your Phone number.';}
if (!empty($_REQUEST['redmapleacer']) || !empty($_REQUEST['chinesepistache']) || !empty($_REQUEST['raywoodash'])) {
$check1 = $_REQUEST['redmapleacer'];
if (empty($check1)){$check1 = 'Unchecked';}else{$check1 = 'Checked';}
$check2 = $_REQUEST['chinesepistache'];
if (empty($check2)){$check2 = 'Unchecked';}else{$check2 = 'Checked';}
$check3 = $_REQUEST['raywoodash'];
if (empty($check3)){$check3 = 'Unchecked';}else{$check3 = 'Checked';}
} else {$errors[] = 'You forgot to enter your Phone number.';}
}
//End of validation
Sends the email if validation passes
The following code is what sends the email. The inputs must pass the previous validation in order for the email to send. You will need to replace the “to” email address with the email address you want to receive the email to.
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From: Our Site!"; //Site name
// Change this to your email address you want to form sent to
$to = "[email protected]";
$subject = "Admin - Our Site! Comment from " . $name . "";
$message = "Message from " . $firstname . " " . $lastname . "
Phone: " . $phone . "
Red Maple Acer: " . $check1 ."
Chinese Pistache: " . $check2 ."
Raywood Ash: " . $check3 ."";
mail($to,$subject,$message,$from);
}
}
?>
Error Reporting Code
<?php
//Print Errors
if (isset($_REQUEST['submitted'])) {
// Print any error messages.
if (!empty($errors)) {
echo '<hr /><h3>The following occurred:</h3><ul>';
// Print each error.
foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
echo '</ul><h3>Your mail could not be sent due to input errors.</h3><hr />';}
else{echo '<hr /><h3 align="center">Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>';
echo "Message from " . $firstname . " " . $lastname . "
Phone: ".$phone."
";
echo "
Red Maple Acer: " . $check3 . "";
echo "
Chinese Pistache: " . $check2 . "";
echo "
Raywood Ash: " . $check3 . "";
}
}
//End of errors array
?>
Prints the contact form
This is the form that will display for the visitor to fill out.
Contact us
Fill out the form below.
You can paste the entire code directly in the body section of your webpage to get it working. We have more tutorials with other methods to send email from your website at the following links.
Using phpMailer to Send Mail through PHP
How to set up FormMail