Lesson 10 - An input field is required

Features

Most request info forms have fields that are required to have input. After all, what is the point of sending a request for info and not including your name.

Request Info Pretty
Click here to see a larger image

Actions

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

Surf to the Lesson10_Required.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 added some HTML to tell the visitor that First Name was a required field.

In the PHP code, we added an if construct to test whether or not the visitor entered at least one character into the contactFirstName field. If they did not, then we print an error message in big bright red letters. We used the exit() function for the printing because it also terminates execution of the PHP script.

  <html>
<head>
  <title>A Pretty Request Info Form</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:
      </td>
      <td width="350">
        <input type="text" name="contactLastName">
      </td>
    </tr>
    <tr>
      <td width="200" class="FormVariableName" align="right">
        Email:
      </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>");
    
  
/* 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 might wonder why the if() statement did not have {}. That is because it works without them and the code here is very simple. In the next lesson, I will show all three methods for the if().

Go To MarketingTactics home