Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
Amazon logo

This tutorial shows how to use a PHP program to receive and process data input by a user in an XHTML form. The sample program also uses the regular-expression capabilities that we presented in the preceding tutorial

[Note: This tutorial is an excerpt (Section 26.5) of Chapter 26, PHP, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.899–900;916-922. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
26.5 Form Processing and Business Logic (Continued)
The action attribute of the form element (line 18) indicates that when the user clicks the Register button, the form data will be posted to form.php (Fig. 26.14) for processing. Using method = "post" appends form data to the browser request that contains the protocol (i.e., HTTP) and the requested resource's URL. Scripts located on the Web server's machine (or on a machine accessible through the network) can access the form data sent as part of the request.
We assign a unique name (e.g., email) to each of the form's input fields. When Register is clicked, each field's name and value are sent to the Web server. Script form.php can then access the value for each specific field through the superglobal array $_POST. Superglobal arrays are associative arrays predefined by PHP that hold variables acquired from the user input, the environment or the Web server and are accessible in any variable scope. The $_ENV array used in Fig. 26.12 is another example of a superglobal array. $_POST contains key-value pairs corresponding to name-value pairs for variables submitted through the form. [Note: The superglobal array $_GET would contain these key-value pairs if the form had been submitted using the HTTP get method.] Figure 26.14 (form.php) processes the data posted by form.html and sends XHTML back to the client.

Good Programming Practice 26.1
Use meaningful XHTML object names for input fields. This makes PHP scripts that retrieveform data easier to understand.
Fig. 26.14 Obtaining user input through forms. (Part 1 of 3.)
1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4   <!-- Fig. 26.14: form.php -->
5   <!-- Read information sent from form.html -->
6   
7   <html xmlns = "http://www.w3.org/1999/xhtml">
8      <head>
9          <title>Form Validation</title>
10     </head>
11
12      <body style = "font-family: arial,sans-serif">
13   
14         <?php
15            extract( $_POST );
16   
17            // determine whether phone number is valid and print
18            // an error message if not
19            if ( !ereg( "^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$",
20               $phone ) ){
21   
22               print( "<p><span style = \"color: red;
23                  font-size: 2em\">
24                  INVALID PHONE NUMBER</span><br />
25                  A valid phone number must be in the form
26                  <strong>(555)555-5555</strong><br />
27                  <span style = \"color: blue\">
28                  Click the Back button, enter a valid phone
29                  number and resubmit.<br /><br />
30                   Thank You.</span></p></body></html>" );
31   
32               die(); // terminate script execution
33            }
34         ?>
35   
36         <p>Hi
37            <span style = "color: blue">
38                <strong>
39                  <?php print( "$fname" ); ?>
40                </strong>
41             </span>.
42             Thank you for completing the survey.<br />
43   
44            You have been added to the
45             <span style = "color: blue">
46                <strong>
47                  <?php print( "$book " ); ?>
48                </strong>
49             </span>
50            mailing list.
51          </p>
52          <strong>The following information has been saved
53             in our database:</strong><br />
54   
55         <table border = "0" cellpadding = "0" cellspacing = "10">
56             <tr>
57                <td bgcolor = "#ffffaa">Name </td>
58               <td bgcolor = "#ffffbb">Email</td>
59               <td bgcolor = "#ffffcc">Phone</td>
60                <td bgcolor = "#ffffdd">OS</td>
61             </tr>
62   
63             <tr>
64               <?php
65   
66                  // print each form field's value
67                  print( "<td>$fname $lname</td>
68                     <td>$email</td>
69                     <td>$phone</td>
70                     <td>$os</td>" );
71                ?>
72             </tr>
73          </table>
74   
75          <br /><br /><br />
76          <div style = "font-size: 10pt; text-align: center">
77             This is only a sample form.
78             You have not been added to a mailing list.
79          </div>
80       </body>
81   </html>

Page 1 | 2 | 3
 
Other PHP Tutorials
PHP Tutorial 1: Introduction to PHP
PHP Tutorial 2: Creating Simple PHP Programs
PHP Tutorial 3: String Processing and Regular Expressions
PHP Tutorial 4: Form Processing and Business Logic (You are here)