Lesson 12 - All input fields are required

Features

Now, our form will have all three (3) fields required.

Request Info Pretty
Click here to see a larger image

Actions

Arrow Red Right Click to see Lesson12_AllRequired.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: Lesson12_AllRequired.txt to your PC. Change the extension from txt to php. Change the “recipient address” and the pointer to the style sheet and then upload to your website.

Surf to the Lesson12_AllRequired.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 all three (3) variations of the if construct to test whether or not the visitor entered data into each required field. The test for contactEmail uses a regular expression function. If data was not input, then we print an error message.

  <html>
<head>
  <title>A Request Info Form with all fields required.</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">
        &nbsp;<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">
      </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">
      </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">
      </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

Isn't is annoying that everytime you get an error message, the form clears itself of all the data you just input? That is why we have the next lesson.

Go To MarketingTactics home