Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

Accessing Form Input with Built-In Arrays

The techniques you have looked at so far work well but can clutter up your scripts with global variables. To limit the number of globals in your script you can disable the feature that creates variables for each of your form fields by setting the register_globals directive to off in the php.ini file. We discussed the php.ini file in more detail in Hour 2, "Installing PHP".

That will clean up your namespace, but how can you access submitted form elements now?

The global variables that PHP 4 makes available provide the solution to this problem. According to whether or not a submitting form used the GET or POST method, you will have access to one or both of $HTTP_GET_VARS or $HTTP_POST_VARS. These are associative arrays that contain the name/value pairs submitted. Listing 9.6 takes advantage of this to list all the fields submitted from a form via a GET request.

Listing 9.6 Reading Input from Any Form Using the $HTTP_GET_VARS array

 1: <html>
 2: <head>
 3: <title>Listing 9.6 Reading input from any form using the $HTTP_GET_VARS array</title>
 4: </head>
 5: <body>
 6: <?php
 7: foreach ( $HTTP_GET_VARS as $key=>$value ) {
 8:  print "$key == $value<BR>\n";
 9: }
10: ?>
11: </body>
12: </html> 

This code lists the names and values of all parameters passed to it via a GET transaction. We could also do the very same thing with the $HTTP_POST_VARS array.

  • + Share This
  • 🔖 Save To Your Account