Accessing Form Value Collections
For those who are running older systems or want to get away from globals altogether, you have the option to use the $HTTP_GET_VARS
and $HTTP_POST_VARS
arrays. These collections are deprecated, but they are still available,
and still in wide use. When they do go away, they'll be replaced by
the $_GET
and $_POST
arrays, added in version 4.1.
Both of these arrays are of a type known as hash tables. A hash table is an array that is indexed by string values rather than integers. In the case of forms, the values are accessible by their name, as shown in Listing 3:
Listing 3. Accessing form values through hash tables
|
You can use this method to retrieve values for each of your fields by name.
One name, multiple values
So far, each name has corresponded to only one value. What happens when there's more than one value? The crew species list box, for example, allows multiple values to be submitted with the name crew.
Ideally,
you'd like these values to be available as an array so you can retrieve
them explicitly. To make that happen, you've got to make a slight
change to the HTML page. Fields that are to be submitted as arrays
should be named with brackets, as in crew[]
:
|
Once you've made this change, retrieving the form value actually yields an array:
Listing 5. Accessing the variables as an array
|
Submitting the page now shows the multiple values:
|
Notice first that this is a zero-based array. The first value encountered is in position 0, the next in position 1, and so on. In this case, I submitted only two values, so the third spot is blank.
Normally, you don't know
how many items will be submitted, so instead of calling each item
directly you can use the fact that it's an array to your advantage,
using the sizeof()
function to determine how many values were submitted:
|
Sometimes, however, the problem is not too many values, but no values at all.
View Using HTML forms with PHP Discussion
Page: 1 2 3 4 5 6 Next Page: The Amazing Disappearing Checkboxes