All the fields sent to your program are automatically stored in a special associa-tive array called $_REQUEST.. Each field name on the original form becomes a key, and the value of that
Trang 1All the fields sent to your program are automatically stored in a special
associa-tive array called $_REQUEST Each field name on the original form becomes a key,
and the value of that field becomes the value associated with that key If you have
a form with a field called userName, you can get the value of the field by calling
$_REQUEST[“userName”]
The $_REQUESTarray is also useful because you can use a foreachloop to quickly
determine the names and values of all form elements known to the program The
formReader.phpprogram source code illustrates how this is done:
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>Form Reader</title>
</head>
<body>
<h1>Form Reader</h1>
<h3>Here are the fields I found on the form</h3>
<?
print <<<HERE
<table border = 1>
<tr>
<th>Field</th>
<th>Value</th>
</tr>
HERE;
foreach ($_REQUEST as $field => $value){
print <<<HERE
<tr>
<td>$field</td>
<td>$value</td>
</tr>
HERE;
} // end foreach
print “</table>\n”;
?>
</body>
</html>
143
l i
Trang 2Note how I stepped through the $_REQUESTarray Each time through the foreach loop, the current field name is stored in the $fieldvariable and the value of that field is stored in $value
I use this script when I’m debugging my programs If I’m not getting the form elements I expected from a form, I put a foreach $_REQUEST loop in at the top
of my program to make sure I know exactly what’s being sent to the program Often this type of procedure can help you find misspellings or other bugs.
Creating a Multidimensional Array
Arrays are very useful structures for storing various kinds of data into the com-puter’s memory Normal arrays are much like lists Associative arrays are like name/value pairs A third special type, a multidimensional array, acts much like table data For instance, imagine you were trying to write a program to help users determine the distance between major cities You might start on paper with a table like Table 5.1
T R I C K 144
g r
s o
l u
g in
e r
Indianapolis New York Tokyo London Indianapolis 0 648 6476 4000
New York 648 0 6760 3470
Tokyo 6476 6760 0 5956
London 4000 3470 5956 0
T A B L E 5 1 D I S T A N C E S B E T W E E N M A J O R C I T I E S
I N THE R EAL W ORLD
PHP provides some other variables related to $_REQUEST The $HTTP_POST_VARS array holds all the names and values sent through a POST request, and
$HTTP_GET_VARS array holds names and values sent through a get request You can use this feature to make your code more secure If you create variables only from the $HTTP_POST_VARS array, for example, all input sent via the get method are ignored This makes it harder for users to forge data by putting field names
in the browser’s address bar Of course, a clever user can still write a form that contains bogus fields, so you always have to be a little suspicious whenever you get any data from the user.
Trang 3It’s reasonably common to work with this sort of tabular data in a computer
pro-gram PHP (and most languages) provides a special type of array to assist in
work-ing with this kind of information The basicMultiArray program featured in
Figures 5.8 and 5.9 illustrates how a program can encapsulate a table
145
l i
FIGURE 5.8
The user can
choose origin and
destination cities
from select groups.
FIGURE 5.9
The program looks
up the distance
between the cities
and returns an
appropriate value.
Trang 4Building the HTML for the Basic
Multidimensional Array
Using a two-dimensional array is pretty easy if you plan well I first wrote out my table on paper (Actually, I have a write-on, wipe-off board in my office for exactly this kind of situation.) I assigned a numeric value to each city:
Indianapolis = 0
New York = 1
Tokyo = 2
London = 3
This makes it easier to track the cities later on
The HTML code builds the two select boxes and a submitbutton in a form
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>Basic multi-dimensional array</title>
</head>
<body>
<h1>Basic 2D Array</h1>
<form action = basicMultiArray.php>
<table border = 1>
<tr>
<th>First city</th>
<th>Second city</th>
<tr>
<!— note each option value is numeric —>
<tr>
<td>
<select name = “cityA”>
<option value = 0>Indianapolis</option>
<option value = 1>New York</option>
<option value = 2>Tokyo</option>
<option value = 3>London</option>
</select>
</td>
146
g r
s o
l u
g in
e r
Trang 5<select name = “cityB”>
<option value = 0>Indianapolis</option>
<option value = 1>New York</option>
<option value = 2>Tokyo</option>
<option value = 3>London</option>
</select>
</td>
</tr>
<tr>
<td colspan = 2>
<input type = “submit”
value = “calculate distance”>
</td>
</tr>
</table>
</body>
</html>
Recall that when the user submits this form, it sends two variables The cityA
variable contains the value property associated with whatever city the user
selected; cityBlikewise contains the value of the currently selected destination
city I carefully set up the value properties so they coordinate with each city’s
numeric index If the user chooses New York as the origin city, the value of $cityA
is 1, because I decided that New York would be represented by the value 1 I’m
giv-ing numeric values because the information is all stored in arrays, and normal
arrays take numeric indices (In the next section I show you how to do the same
thing with associative arrays.)
Responding to the Distance Query
The PHP code that determines the distance between cities is actually quite simple
once the arrays are in place:
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>Distance calculator</title>
</head>
<body>
147
l i