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

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



<?

$ship_value = $HTTP_GET_VARS['ship'];
echo $ship_value;
echo "<br />";

$tripdate_value = $HTTP_GET_VARS['tripdate'];
echo $tripdate_value;
echo "<br />";

$exploration_value= $HTTP_GET_VARS['exploration']; 
echo $exploration_value;
echo "<br />";

$contact_value = $HTTP_GET_VARS['contact'];
echo $contact_value;
?>

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[]:

Listing 4. Modifying the HTML page


...
       <td>
           <select name="crew[]" multiple="multiple">

                   <option value="xebrax">Xebrax</option>
                   <option value="snertal">Snertal</option>
                   <option value="gosny">Gosny</option>
           </select>

       </td>
...

Once you've made this change, retrieving the form value actually yields an array:

Listing 5. Accessing the variables as an array


...
$crew_values = $HTTP_GET_VARS['crew']; 
echo "0) ".$crew_values[0];
echo "<br />";
echo "1) ".$crew_values[1];
echo "<br />";
echo "2) ".$crew_values[2];
...

Submitting the page now shows the multiple values:


0) snertal
1) gosny
2)

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:

Listing 6. Determining the size of the array


...
for ($i = 0; $i < sizeof($crew_values); $i++) {
    echo $crew_values[$i];
    echo "<br />";
}
...

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

First published by IBM developerWorks


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