Specifications
There are two ways of accessing the form data via variables.
In this example, and throughout this book, we have used the short style for referencing form
variables. In this case, you will notice that the variable names we use in this script are the same
as the ones in the HTML form. This is always the case with the short style. You don’t need to
declare the variables in your script because they are passed into your script, essentially as argu-
ments are passed to a function. If you are using this style, you can, for example, just begin
using a variable like $tireqty as we have done previously.
The second style is to retrieve form variables from one of the two arrays stored in
$HTTP_POST_VARS and $HTTP_GET_VARS. One of these arrays will hold the details of all the
form variables. Which array is used depends on whether the method used to submit the form
was
POST or GET, respectively.
Using this style to access the data typed into the form field
tireqty in the previous example,
you would use the expression
$HTTP_POST_VARS[“tireqty”]
You will only be able to use the short style if you have set the register_globals directive in
your php.ini file to “On”. This is the default setting in the regular php.ini file.
If you want to have register_globals set to “Off”, you will have to use the second style. You
will also need to set the track_vars directive to be “On”.
The longer style will run faster and avoid automatically creating variables that might not be
needed. However, the shorter style is easier to read and use and is the same as in previous ver-
sions of PHP.
Both of these methods are similar to ones used in other scripting languages such as Perl, and
might seem familiar.
You might have noticed that we don’t, at this stage, check the variable contents to make sure
that sensible data has been entered in each of the form fields. Try entering deliberately wrong
data and observing what happens. After you have read the rest of the chapter, you might want
to try adding some data validation to this script.
String Concatenation
In the script, we used echo to print the value the user typed in each of the form fields, followed
by some explanatory text. If you look closely at the echo statements, you will see that the vari-
able name and following text have a period (.) between them, such as this:
echo $tireqty.” tires<br>”;
Using PHP
P
ART I
20
03 7842 CH01 3/6/01 3:39 PM Page 20