Lesson 02 - Your first Request Info form

Features

This form accepts a name and an email address and sends an email to you.

Actions

Arrow Red Right Click to see Lesson02_FirstForm.php in action on this tutorial site. Note: if you enter a name and an email in this form, you will send me an email.
A picture of the Request Info form.

Download this file: Lesson02_FirstForm.txt to your PC. Change the extension to .php. Change the “recipient address” and then upload to your website.

Surf to the Lesson02_FirstForm.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
Name = your name
Email = sender@sendersdomain.com

Gotchas

I used to have the words below, but then my ISP upgraded and used the default istallation for PHP. So the source code is indicative of “register_globals = off”.

(Old words: If you did not receive an email or if the contents were blank, then perhaps this is true: “In PHP 4.2.0 and later, the default value for the PHP directive register_globals is off. This is a major change in PHP. Having register_globals off affects the set of predefined variables available in the global scope.” You need to run the phpinfo() function on your website and check that register_globals is set to "on".)

If your version of PHP is below 4.3.x, then call your hosting company. They need to upgrade to a newer version of PHP.

Source Code

The code: action="<?php echo($PHP_SELF); ?>" tells PHP to execute the PHP code on THIS page after the submit button has been clicked. More about PHP_SELF.

What we do know is that all the typed in values are available in a “predefined variable”: $_POST. Therefore, we can get the value of the HTML name “contactName” from $_POST['contactName']. Note, that with register_globals turned on, $contactName and $_POST['contactName'] have the same value.

The hidden variable $firstPass and the associated if() test are here so that the page will not send an email the first time it is loaded. The email is only sent when the submit button is clicked.

If PHP is installed on your webserver, then download this file: Lesson02_FirstForm.txt to your PC. Change the file extension to .phps and upload the file to your webserver. View the file with your browser and you should see code identical to that shown below.

  <html>
<head>
<title>First Request Info Form</title>
</head>

<body>

<form method="POST" name="FirstEmailForm" action="<?php echo($PHP_SELF); ?>" >
  <p>
    Your Name:<input type="text" name="contactName">
  </p>
  <p>
    Your Email:<input type="text" name="contactEmail">
  </p>
  <p>
    <input type="hidden" name="firstPass" value="No">
    <input type="submit" name="subRequestButton" value="Send me more info">
  </p>
</form>

<?php
/* Set variables equal to their "posted" values". We need to do this because "register_globals = off" is the default. */
$contactName = $_POST['contactName'];
$contactEmail = $_POST['contactEmail'];
$firstPass = $_POST['firstPass'];

if($firstPass == "No")
{
  
/* Construct the email message  */
  
$messageToAcme "Please send me more information about Acme products";
  
$messageToAcme .= "\n\n Name = " $contactName;
  
$messageToAcme .= "\n Email = " $contactEmail;
  
$mailHeaders "From: \"".$contactName."\"<".$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

If all went well, then you have learned three things:

  1. That success can be dependent upon how PHP is installed on our webserver.
  2. How to have a simple form collect input and send an email.
  3. You should assume that register_globals is OFF as that is the defualt and more and more ISPs will install it that way.
Go To MarketingTactics home