The Amazing Disappearing Checkboxes
It's important to realize that a checkbox is only submitted if it's
actually checked. Otherwise, its very absence tells you what you need
to know: the user didn't click this checkbox. In the case of
checkboxes, you can check explicitly to see whether the value was set
using the isset()
function:
|
Getting all form values
Checkbox fields are just one example of a situation where you may not be entirely certain of the names of form values to expect. Often, you might find it useful to have a single routine that look at all of your form values in a generic way.
Fortunately, because $HTTP_GET_VARS
and its ilk are simply hash tables, you can use some of the properties
of arrays to manipulate them. For example, you can use the array_keys()
function to get a list of all of the potential value names:
|
In this case, you actually combine several techniques. First, retrieve an array of form field names and call it $form_fields
. The $form_fields
array is just a typical array, so you can use the sizeof()
function to determine the number of potential keys, and loop through
each one. For each one, you retrieve the name of the field and then
use that name to get the actual value. The result is a Web page that
reads:
|
Notice two important things
here. First, the contact field didn't return at all, as expected.
Second, the crew value (which is, incidentally, named crew and not crew[]
,
as you might expect) is an array, and not a value. To actually
retrieve all values, you'll need to detect any arrays using the is_array()
function and handle them accordingly:
|
The result is all of the data that was actually submitted:
|
View Using HTML forms with PHP Discussion
Page: 1 2 3 4 5 6 Next Page: One Final Note: Dots