Ïðèãëàøàåì ïîñåòèòü
ßçûêîâ (yazykov.lit-info.ru)

Reading Out Form Data

Previous
Table of Contents
Next

Reading Out Form Data

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:

  • $HTTP_GET_VARS All data provided using GET

  • $HTTP_POST_VARS All data provided using POST

  • $HTTP_REQUEST_VARS All data provided using GET or POST, or via cookies (use not recommended)

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:

  • $_GET for GET data

  • $_POST for POST data

  • $_REQUEST for POST, GET, and cookies (not recommended)

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.

Table 4.1. Form Field Types and Data Returned in $_GET/$_POST

Form Field Type

Data Returned

Text field

Text in field

Password field

Text in field (clear text, not encrypted)

Multiline text field

Text in field

Hidden field

value attribute of field

Radiobutton

value attribute of selected radio button

Checkbox

value attribute of check box if checked (or "on", if value not set)

Selection list

value attribute of selected list element (or caption of selected list element, if value not set)

Multiple selection list

value attributes of selected list elements as san array (or captions of selected list elements as an array, if values not set)

Submit button

value attribute of Submit button, if this one was used to send the form (important if there is more than one Submit button)


TIP

Two remaining form field types, graphical Submit buttons and file uploads, are covered specifically later in this chapter.



Previous
Table of Contents
Next