Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > USING HTML FORMS WITH PHP


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Using HTML forms with PHP
By Nicholas Chase - 2003-12-12 Page:  1 2 3 4 5 6

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:

Listing 7. Checking to see if a checkbox was submitted


...
$contact_value = $HTTP_GET_VARS['contact']; 
echo $contact_value;

if (isset($contact_value)) {
  //The checkbox was clicked
} else {
  //The checkbox wasn't clicked
}
...

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:

Listing 8. Getting a list of form value names



...
$form_fields = array_keys($HTTP_GET_VARS);
for ($i = 0; $i < sizeof($form_fields); $i++) {

    $thisField = $form_fields[$i];
    $thisValue = $HTTP_GET_VARS[$thisField];
    echo $thisField ." = ". $thisValue;
    echo "<br />";  

}
...

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:


ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = Array

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:

Listing 9. Accounting for arrays


...
for ($i = 0; $i < sizeof($form_fields); $i++) {

    $thisField = $form_fields[$i];
    $thisValue = $HTTP_GET_VARS[$thisField];
    if (is_array($thisValue)){
        for ($j = 0; $j < sizeof($thisValue); $j++) {
            echo $thisField ." = ". $thisValue[$j];
            echo "<br />";
        }
    } else {
        echo $thisField ." = ". $thisValue;
    }

    echo "<br />";  

}
...

The result is all of the data that was actually submitted:


ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = snertal
crew = gosny



View Using HTML forms with PHP Discussion

Page:  1 2 3 4 5 6 Next Page: One Final Note: Dots

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.