Displaying All the Data in a Form


You can use PHP to display all a form’s data. Following is an example, formdata.html, which includes a form containing a text field:

 <html>   <head>       <title>Reading All Form Data</title>   </head>   <body>     <center>       <h1>Reading All Form Data</h1>       <form method="post" action="formdata.php">         Please enter your name:         <input name="name" type="text">         .         .         .         <br>         <br>         <input type="submit" value="Submit">       </form>     </center>   </body> </html>

as well as a <select> control:

 <html>   <head>       <title>Reading All Form Data</title>   </head>   <body>     <center>       <h1>Reading All Form Data</h1>       <form method="post" action="formdata.php">         Please enter your name:         <input name="name" type="text">         <br>         <br>         Select your favorite sandwiches(s):         <br>         <br>         <select name="sandwiches[]" multiple>           <option>Ham</option>           <option>Turkey</option>           <option>Salami</option>           <option>Chicken</option>         </select>         <br>         <br>         <input type="submit" value="Submit">       </form>     </center>   </body> </html>

To read all the data in this form, you use a special form of the foreach loop in a new PHP page, formdata.php:

 <html>   <head>     <title>       Reading all form data     </title>   </head>   <body>     <center>       <h1>Reading all form data</h1>       Here is the form's data:       <br>         .         .         .     </center>   </body> </html>

You can loop over an array with string indices rather than numeric indices, such as the $_REQUEST array, using a foreach loop like this: foreach($_REQUEST as $index => $value), which places the index in $index and the associated value in $value each time through the loop:

 <html>   <head>     <title>       Reading all form data     </title>   </head>   <body>     <center>       <h1>Reading all form data</h1>       Here is the form's data:     <br>     <?       foreach($_REQUEST as $index => $value){       .       .       .       }     ?>     </center>   </body> </html>

Note that some values in the $_REQUEST array can be arrays themselves (as with multiple-selection <select> controls), in which case you need another foreach loop:

 <html>   <head>     <title>       Reading all form data     </title>   </head>   <body>     <center>       <h1>Reading all form data</h1>       Here is the form's data:       <br>       <?         foreach($_REQUEST as $index => $value){           if(is_array($value)){             foreach($value as $item){               echo $index, " => ", $item, "<br>";             }           }           .           .              .         }       ?>       </center>   </body> </html>

Otherwise, you can just display the current $index/$value pair:

 <html>   <head>     <title>       Reading all form data     </title>   </head>   <body>     <center>       <h1>Reading all form data</h1>       Here is the form's data:       <br>       <?         foreach($_REQUEST as $index => $value){           if(is_array($value)){             foreach($value as $item){               echo $index, " => ", $item, "<br>";             }           }           else {             echo $index, " => ", $value, "<br>";           }         }       ?>       </center>   </body> </html>

Formdata.html, crammed full of data, is illustrated in Figure 14.1.

image from book
Figure 14.1: The formdata.html page

When you click the Submit button, all the form’s data appears, as shown in Figure 14.2.

image from book
Figure 14.2: The formdata.php application



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net