Displaying All a Form's Data At OnceWe'll start this chapter with a quick onedisplaying all the data a form sends to your web application. When you're creating a web application and things aren't going right, you can use a PHP page like the one we'll develop here to see what's actually being sent to your code. As an example, we'll stock an HTML page with a number of controls, as you see in phpformdata.html, Example 6-1. You can see what this sample page looks like in Figure 6-1. Example 6-1. Submitting data in a form, phpformdata.html<HTML> <HEAD><TITLE>Reading All Form Data</TITLE></HEAD> <BODY> <CENTER><H1>Reading All Form Data</H1> <FORM METHOD="POST" ACTION="phpformdata.php"> What's your name?<INPUT NAME="Name" TYPE="TEXT"> <BR><BR> Select your favorite fruit(s): <SELECT NAME="Food[]" MULTIPLE> <OPTION>Apple</OPTION> <OPTION>Orange</OPTION> <OPTION>Pear</OPTION> <OPTION>Pomegranate</OPTION> </SELECT> <BR><BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </CENTER> </BODY> </HTML> Figure 6-1. Submitting form data.To read the data sent by the user, just use a foreach loop over $_REQUEST. If a particular data item is itself an array, we'll use a nested foreach to display its data, as you see in phpformdata.php, Example 6-2. Example 6-2. Displaying all the data in a form, phpformdata.html<HTML> <HEAD><TITLE>Retrieving Data From Forms</TITLE></HEAD> <BODY><CENTER> <H1>Retrieving Data From Forms</H1> Here is the data from the form:<BR> <?php foreach($_REQUEST as $key => $value){ if(is_array($value)){ foreach($value as $item){ echo $key, " => ", $item, "<BR>"; } } else { echo $key, " => ", $value, "<BR>"; } } ?> </CENTER></BODY> </HTML> You can see the results in Figure 6-2, where all form data is displayed. Figure 6-2. Reading data from forms. |