How to Make a Validated PHP E-mail Form
November 5th, 2008
If you want the code to add a validated form for users to fill out on your website, you’ve come to the right place.
There are a million solutions to this problem posted on the web, and I’ve found that many of them don’t work, like “mailto:youremail@example.com”, or the solution they offer is a paid service.
After spending hours one weekend reading a PHP book and patching code from others on the internet this is the optimal solution I use.
First, lets go over the PHP terms used in the code:
$example - variable “example”, in PHP variables are noted by a “$”
isset($example) - determines if variable $example holds any data
!preg_match(’/@/i’,$example) - checks to see if an “@” sign is included in a text string. This will be used to check that someone has entered a valid e-mail address.
$howlong = strlen($REQUEST["example"]) - this will make a variable called “howlong” with a value equal to the number of characters in the “example” string.
This is sample PHP code that goes at the very top of the page’s code:
<?php
$first = strlen($_REQUEST["firstname"]);
$second = $_REQUEST["email"];
$third = strlen($_REQUEST["msg"]);
$fourth = strlen($_REQUEST["subject"]);
$contactfor = $_REQUEST["msg"];
if (isset($second) && !preg_match(’/@/i’,$second)) {
$condition = TRUE;
} else {
$condition = FALSE;
}
if ($first >= 2 && $condition == FALSE && $third >= 5) {
$redirectpage = “index.html”;
$errorpage = “error.php”;
$to = “youremail@yourprovider.com”;
$lastname = $_POST['lastname'];
$replyTo = $_POST['email'];
$from = $_POST['firstname'] . ” ” . $_POST['lastname'];
$subject = “Referral”;
$message = stripslashes(”Name: ” . $from . “\n\n” . “Email: ” . $_POST['email'] . “\n\nPhone: “. $_POST['phone'] . “\n\n” . “Message: ” . $contactfor );
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
header(”Location: $redirectpage”);
} else {
header(”Location: $errorpage”);
}
} ?>
The is the code you need to add to your HTML form:
All the textboxes and the submit button for your form must go inside similar tags:
<form action=”" method=”post” name=”form1″ id=”form1″>
text boxes and submit buttons etc.
</form>
The reason the form action is “” is because the page will reload, and either deliver the message and move on to the $redirect page, or produce error messages next to the textboxes “first name is required.”
This is the code to add a textbox for the ”subject” line of your e-mail:
<input name=”subject” type=”text” id=”subject”
value=”<?php $subject = $_POST["subject"]; echo $subject; ?>”
size=”25″ maxlength=”40″/>
Change the word “subject” in the above code to whatever variable you would like to recieve from your users. The $subject is a variable that will draw the text from what someone enters in the “subject” textbox. “echo $subject;” is used for validation. If someone doesn’t fill out your form correctly the page will reload and the data they entered into the textboxes will still be there.
Finally, next to the textboxes put this code (customized to each field):
<?php
if(isset($subject) && $fourth <= 2) {
echo “<font color=’#FF0000′>Subject required.”;
} ?>
If someone doesn’t fill out a text field, the message “subject (or whatever field you enter here) required” in red text.
Of course, save your code as a PHP file (.php), not .html!
There is quite a bit of code here and implemnting it may take considerable reflection on your part. If you have any questions or comments to improve the code, please let me know, I’d be happy to respond.
Visit our contact page if you want to see a live demo. Type in an e-mail address without a “@” or don’t fill out a required field and you’ll see the validation in action.
[...] a post I wrote on a PHP script for forms, I used the title “How To Make a Validated PHP Email Form“. A solid number of people search for the information this way, and I have received positive [...]