At the beginning, reading out form data was very easy: If the form field had the name attribute "whatever" or, in newer versions of HTML/XHTML, the id attribute "whatever", PHP creates a variable $whatever in the global scope. This is very convenient, but, from an architectural point of view, is a bad idea. Therefore, this was disabled by default from PHP version 4.2 onward, using the following php.ini directive: register_globals = Off Since PHP 3, the following global arrays existed for form data:
These arrays are global; therefore, you have to use the global keyword to uplevel them to global scope if you use them within a function: function processData() { global $HTTP_POST_VARS; // now you may access $HTTP_POST_VARS } However, these arrays can be deactivated (PHP 5 onward), as well, using this php.ini directive: register_long_arrays = Off Therefore, the following is the only recommended method to access form data today in PHP:
The keys of these arrays are the names of the form values. The $_* arrays are so-called superglobal arraysthat is, you do not have to use the global keyword to get them into global scope; they are already available within functions. When you have decided which superglobal array to use (depending on the form's method), accessing form data is easy: $_GET[<formfieldname>] or $_POST[<formfieldname>] retrieves the value in the form element. Table 4.1 shows which data is returned for which form field type.
|