Lesson 17 - The Thank You page

Features

Up until this form, when you clicked on the submit button, nothing visibly happened. Yes, behind the scenes an email was being sent, but you could not tell that.

Request Info Pretty
Click here to see a larger image

Your visitor also face the issue of “what do I do now?”. I clicked on the submit button but nothing happend.

The solution is: a Thank You page. That is, upon successful completion of the invisible actions, you forward (i.e., redirect) the visitor to a page thanking them for requesting info.

Actions

Arrow Red Right Click to see Lesson17_ThankYou.php in action on this tutorial site. Note: if you enter name an email in this form, you will send me an email.

Download these three (3) files: Lesson17_ThankYou.txt, Thanks_Request_Info_Success.html and Thanks_Request_Info_Failure.html to your PC. Change the txt extension to php. Change the “recipient address” in the Lesson17 file and the pointers to the style sheet and then upload all to your website. The “thank yous” need to be in separate directory.

Surf to the Lesson17_ThankYou.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

And, you should be sent to the Thank You page.

Gotchas

“Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Note: In PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.”

Source Code

The all important functions here are ob_start and ob_flush—the first and last items on the page, repectively. You must put the entire page into an output buffer or your redirect using header won’t work.

  <?php
  ob_start
();  /* Start buffer so we can use header anywhere in the page  */
?>
  
<html>
<head>
  <title>A Request Info Form with a Thank You.</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']; ?>" >
        &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" 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  */
  
if (mail("dave@marketingtactics.com""Please Send Acme Info"$messageToAcme$mailHeaders))
  {
    
header("Location: ../../ThankYous/Thanks_Request_Info_Success.html");  /* Redirect browser */
    
exit;                                                  /*   Make sure that code below does not get executed when we redirect. */
  
}
  else
  {
    
header("Location: ../../ThankYous/Thanks_Request_Info_Failure.html");
    
mail("webmaster@marketingtactics.com""Acme, Request Info Failure"$messageToAcme$mailHeaders);
    exit;
  }
}
?>


</body>
</html>
<?php
  ob_end_flush
();
?>

Final Thoughts

None.

Go To MarketingTactics home