Larger Text Normal Text Smaller Text

IT 280 Web Development II, Spring 2008

 

Processing HTML Forms

Background

HTML Forms

The form action

Example 1

We will look at how to process information entered into the following form

      <form action="process_form1.php">
      
            <!-- When the submit button is clicked, send -->
            <!-- form data to the PHP page process_form1.php -->
      
      <input type="text"
             name="username"
             value="Type in your name" />
      
      <input type="text"
             name="the_word"
             value="Enter word of the day" />
      
      <input type="submit" /><input type="reset" />
      </form>
      
      

Getting the information from a form

URL-encoding

PHP form variables

Example 1 (continued)

   <html>
   <head><title>First PHP form processing</title></head>
   <body>
   <h1>Processing a Form with PHP</h1>
   <h2>The values entered into the form:</h2>
   <p>Hi there, <?php echo $_GET["username"]; ?>.<br />
   Your word is <?php echo $_GET["the_word"]; ?>.
   </p>

   <h2>Some environment variables:</h2>  
   <p>This document is located at 
       http://<?php echo $_SERVER["HTTP_HOST"], $_SERVER["PHP_SELF"]; ?>.<br />
   You requested it from the page <?php echo $_SERVER["HTTP_REFERER"]; ?>.
   </p>
   
   <p>The HTTP request method used is 
   <?php echo $_SERVER["REQUEST_METHOD"]; ?>.<br />
   The complete query string is 
   <?php echo $_SERVER["QUERY_STRING"]; ?>.
   </p>
   
   </body>
   </html>

The directory for example 1 comtains

Example 2

In the example1, the form was in one document and the processing PHP in another. Often it is convenient to have the form and the PHP in the same document.

   <h1>Process a form with PHP</h1>
    <h2>The form</h2>
   <form action="form2.php">
   
         <!-- When the submit button is clicked, send -->
         <!-- form data to the PHP page process_form.php -->
   
      <p><input type="text"
             name="username"
             value="Type in your name" /></p>
      
      <p><input type="text"
             name="the_word"
             value="Enter word of the day" /></p>
      
      <p><input type="submit" /><input type="reset" /></p>
   </form>
   <h2>The results</h2>
   <p>Value of username: <?php echo $_GET["username"]; ?> </p>  
   <p>Value of the_word: <?php echo $_GET["the_word"]; ?> </p>  

The directory for example 2 comtains

Example 3

Before the form in example 2 is submitted, there aren't any results to show.

This version only shows the results if there are values.

   <h1>Process a form with PHP - if there are data</h1>
    <h2>The form</h2>
   <form action="form3.php">
   
         <!-- When the submit button is clicked, send -->
         <!-- form data to the PHP page process_form.php -->
   
      <p><input type="text"
             name="username"
             value="Type in your name" /></p>
      
      <p><input type="text"
             name="the_word"
             value="Enter word of the day" /></p>
      
      <p><input type="submit" /><input type="reset" /></p>
   </form>

   <p><?php
    if ($_GET["username"] != "" || $_GET["the_word"] != "") 
     {
        echo "<h2>The results</h2>";
        echo "<p>Value of username:" , $_GET["username"], "</p>";
        echo "<p>Value of the_word:" , $_GET["the_word"], "</p>"; 
      }?> 
   

The directory for example 3 comtains

HTTP Request Methods

Example 4

   <body>
   <?php
       if ($_POST["username"] != "" || $_POST["comments"] != "") {
       //If some form data are submitted, tell it back
       
         //the echoed items are separated by commas.
         //The variables are not inside quotes
         echo "<p>Hi there, ", 
              $_POST['username'], 
              ". </p><p>Your comments are: <br /> ",
              $_POST['comments']. "</p>
              ";
       }
       else {
       //If nothing submitted, show them the form
       
         //The PHP string is enclosed in single quotes 
         // because the HTML requires double.
         echo '
           <form action="form4.php" method="post">
               <p>Your name: <input type="text"
                      name="username"
                      value="" />
               <br /> 
               Comments:<br /><textarea name="comments" 
                                   rows="5" cols="60"></textarea><br />
              <input type="submit"/> <input type="reset"/></p>
       
           </form>
           ';
       }
   ?>
   <hr />
   <p>This document is located at: 
       http://<?php echo $_SERVER["HTTP_HOST"], $_SERVER["PHP_SELF"]; ?>.<br />
     The complete query string was: <?php echo $_SERVER["QUERY_STRING"]; ?>
   </p>
   </body>

The directory for example 4 comtains

Lab Exercise 11

  1. Make a PHP document page that includes a form.
  2. The form should have the following controls:
    • a text input for the user's name
    • a text input for favorite color
    • a text input for least favorite food
  3. The PHP in the page will process the form when submitted (adapt the examples).
    • Tell the user about their entered color and food. (____, you really like the color ____ and you really dislike the food ____.)
  4. Ater you are finished with the document, create a second copy of the file, but give it the extension .txt

  5. Add an entry to your table of contents page with links to both the .php and .txt files.

Be sure to add an entry for the lab to your table of contents page and to complete the submission on Blackboard.

Comments to: chang@hood.edu
Last Modified: 08 April 2008. 16:10
Valid CSS! Valid XHTML 1.0!

 

Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.