Team LiB
Previous Section Next Section

A Simple Feedback Form

A simple feedback form usually contains fields for the respondent's name and e-mail address, and a text area for some sort of message. In this section, you create two files: one for the feedback form, and one for the PHP script to process the form, send the mail, and return a response to the browser.

Creating the Feedback Form

In this section, you create the first half of the form/script combo—the feedback form itself, often referred to as the front-end form.

  1. Open a new file in your text editor.

  2. Type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Simple Feedback Form</TITLE>
    </HEAD>
    <BODY>
    
  3. Begin your form. Assume that the method is POST and the action is a script called send_simpleform.php:

    <FORM METHOD="POST" ACTION="send_simpleform.php">
    
  4. Create an input field for the user's name with a text label:

    <P><strong>Your Name:</strong><br>
    <INPUT type="text" NAME="sender_name" SIZE=30></P>
    
  5. Create an input field for the user's e-mail address with a text label:

    <P><strong>Your E-Mail Address:</strong><br>
    <INPUT type="text" NAME="sender_email" SIZE=30></P>
    
  6. Create a text area to hold the message with a text label:

    <P><strong>Message:</strong><br>
    <TEXTAREA NAME="message" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></P>
    
    
  7. Add a submit button:

    <P><INPUT TYPE="submit" NAME="submit" VALUE="Send This Form"></P>
    
  8. Close your form and add some more HTML so that the document is valid:

    </FORM>
    </BODY>
    </HTML>
    
  9. Save the file with the name simple_form.html, and place this file in the document root of your web server.

  10. Open your web browser and type http://127.0.0.1/simple_form.html.

Click To expand

You should see a form containing a text field for the person's name, a text field for the person's e-mail address, a text area for the message, and a button that says Send This Form.

In the next section, you create the back-end script. That script will expect three variables: $_POST[sender_name], $_POST[sender_email], and $_POST[message].

Creating a Script to Mail Your Form

According to the form action in simple_form.html, you need a script called send_simpleform.php. The goal of this script is to accept the text in $_POST[sender_name], $_POST[sender_email], and $_POST[message] format, send an e-mail, and display a confirmation to the web browser.

  1. Open a new file in your text editor.

  2. Begin a PHP block, and then add some error-checking code into the mix:

    <?
    if (($_POST[sender_name] == "") ||
        ($_POST[sender_email] == "") ||
        ($_POST[message] == "")) {
         header("Location: simple_form.html");
         exit;
    }
    
  3. Start building a message string, which will contain the content for the e-mail itself:

    $msg = "E-MAIL SENT FROM WWW SITE\n";
    
  4. Continue building the message string by adding an entry for the sender's name:

    Click To expand
    Note 

    The next few steps will continue building the message string by concatenating smaller strings to form one long message string.

  5. Continue building the message string by adding an entry for the sender's e-mail address:

    $msg .= "Sender's E-Mail:\t$_POST[sender_email]\n";
    
  6. Continue building the message string by adding an entry for the message:

    $msg .= "Message:\t$_POST[message]\n";
    
    
  7. Create a variable to hold the recipient's e-mail address (substitute your own):

    $to = "you@youremail.com";
    
  8. Create a variable to hold the subject of the e-mail:

    $subject = "Web Site Feedback";
    
  9. Create a variable to hold additional mail headers:

    $mailheaders = "From: My Web Site <genericaddress@yourdomain.com>\n";
    
    
    Note 

    Mail headers are the strings at the beginning of mail messages that formulate their structure and essentially make them valid mail messages.

  10. Add the following to the $mailheaders variable:

    $mailheaders .= "Reply-To: $_POST[sender_email]\n";
    
  11. Add the mail() function:

    mail($to, $subject, $msg, $mailheaders);
    
  12. Close your PHP block:

    ?>
    

    Although this code will send the mail, you should return something to the user's screen so that he knows the form has been sent. Otherwise, he might sit there and continually click the Send This Form button.

    Click To expand
  13. Start the HTML output:

    <HTML>
    <HEAD>
    <TITLE>Simple Feedback Form Sent</TITLE>
    </HEAD>
    <BODY>
    
  14. Add some information to tell the user what has happened:

    <H1>The following e-mail has been sent:</H1>
    
  15. Add the text label for the Your Name field and display the user's input, and do the same for the other fields:

    <P><strong>Your Name:</strong><br>
    <? echo "$_POST[sender_name]"; ?>
    <P><strong>Your E-Mail Address:</strong><br>
    <? echo "$_POST[sender_email]"; ?>
    <P><strong>Message:</strong><br>
    <? echo "$_POST[message]"; ?>
    
  16. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
  17. Save the file with the name send_simpleform.php, and place this file in the document root of your web server. Your code should look something like the following figure.

In the next section, you submit the form and see all these different types of string functions at work.

Click To expand

Submitting Your Form and Getting Results

Now that you've created both a front-end form and a back-end script, it's time to try them out.

  1. Open your web browser and type http://127.0.0.1/simple_form.html.

  2. Type your name in the Your Name field.

  3. Type your e-mail address in the Your E-Mail Address field.

  4. Type the following message in the Message field:

    PHP is so cool!
    
  5. Click on the Send This Form button.

The information you entered, along with a confirmation that your e-mail has been sent, will appear.

Now check your e-mail, and see if a message is waiting for you.

Click To expand
Click To expand
  1. Open send_simpleform.php in your text editor.

  2. Modify the string containing Sender's Name by replacing the tab character (\t) with two spaces:

    $msg .= "Sender's Name:
    $_POST[sender_name}\n";
    
    
  3. Modify the string containing Sender's E-Mail by replacing the tab character (\t) with four spaces:

    $msg .= "Sender's E-Mail:    $_POST[sender_email]\n";
    
  4. Modify the string containing Message by replacing the tab character (\t) with 10 spaces:

    $msg .= "Message:       $_POST[message]\n";
    
  5. Save the file and upload it to your server.

Click To expand

In the next section, you create custom error messages for when fields are blank, and you streamline the two-step process of sending mail into one cohesive script.


Team LiB
Previous Section Next Section