Lesson 15 - Saving the input when you have errors
Features
Our form will has all three (3) fields required. But, every time you get an error message for missing or invalid input, all the previously typed-in text disappears.
How do we remedy this?
|

Click here to see a larger image
|
Actions
 |
Click to see Lesson15_SaveInput.php in action on this tutorial site. Note: if you enter name an email in this form, you will send me an email. |
Download this file: Lesson15_SaveInput.txt to your PC. Change the recipient address and the pointer to the style sheet and then upload to your website.
Surf to the Lesson15_SaveInput.php file on your website with your browser. You should see results similar to the image above on the right.
After completing the form and clicking Send me more info, you should receive an email with this in the body:
Please send me more information about Acme products
First Name = your first name
Last Name = your last name
Email = sender@sendersdomain.com
Gotchas
None that I know of.
Source Code
We use value="<?php print $_POST['contactFirstName']; ?>" to keep the inputted data in the fields after an error message has been displayed. You might wonder why we did not use value="<?php print $contactLastName; ?> to accomplish the same objective. Remember, this webserver has register_glabals set to OFF and therefore, you must use $_POST.
| |
<html>
<head>
<title>A Request Info Form with all input saved.</title>
<link rel="stylesheet" href="/PHP_Tutorials/Styles/PHP_Tutorials_Forms_Style.css" type="text/css">
</head>
<body>
<h1>Request Information from the Acme Company</h1>
<form method="POST" name="FirstEmailForm" action="<?php echo($PHP_SELF); ?>" >
<table width="550" border="0" cellspacing="0" cellpadding="0" bgcolor="#DFDFDF">
<tr>
<td colspan="2" class="FormCategoryName">About You</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
First Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactFirstName" value="<?php print $_POST['contactFirstName']; ?>" >
<span class="Footer" style="color:red">* = Required</span>
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Last Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactLastName" value="<?php print $_POST['contactLastName']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Email:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactEmail" value="<?php echo $_POST['contactEmail']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
</td>
<td width="350">
<input type="hidden" name="firstPass" value="No">
<input type="submit" name="subRequestButton" value="Send me more info">
<br>
<span class="Footer">Please click only once.</span>
</td>
</tr>
</table>
</form>
<?php
/* Set variables equal to their "posted" values". We need to do this because "register_globals = off" is the default. */
$contactFirstName = $_POST['contactFirstName'];
$contactLastName = $_POST['contactLastName'];
$contactEmail = $_POST['contactEmail'];
$firstPass = $_POST['firstPass'];
if($firstPass == "No")
{
/* If the user skipped required fields or entered invalid values, write an appropriate error message. */
if ($contactFirstName == "")
exit("<p class='PhpError'>Your First Name is missing.</p>");
if ($contactLastName == "")
{
exit("<p class='PhpError'>Your Last Name is missing.</p>");
}
if (!(eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$",$contactEmail))):
exit("<p class='PhpError'>Email Address appears to be invalid.</p>");
endif;
/* Construct the email message */
$messageToAcme = "Please send me more information about Acme products";
$messageToAcme .= "\n\n First Name = " . $contactFirstName;
$messageToAcme .= "\n Last Name = " . $contactLastName;
$messageToAcme .= "\n Email = " . $contactEmail;
$fullName = $contactFirstName ." " .$contactLastName;
$mailHeaders = "From: \"".$fullName."\"<".$contactEmail.">\n";
$mailHeaders .= "Reply-to: ".$contactEmail;
/* You need to change dave@marketingtactics.com to YOUR email address */
mail("dave@marketingtactics.com", "Please Send Acme Info", $messageToAcme, $mailHeaders);
}
?>
</body>
</html>
|
Final Thoughts
You clicked on the Send Me Info, but nothing happened? How do you know that it worked? See the next lesson for an answer.
|