PHP and MySQL Programming/Form Handling

In order to get user input without using the command line, then you will use HTML forms. PHP has various functions and techniques for handling the input from HTML forms. In specific, we will use the special array variables $_GET[] and $_POST[].

HTML Forms
Before we start using these special variables, lets have a look at how to set up the HTML forms to interact with PHP. Here is a simple example form:

The above example sets up a form with a  and an   attribute. The `method` defines how the data from the form is to be transferred to the page defined by `action`. The `method` can either be GET or POST. If GET is used, then all the form elements will be appended to the url of the target page (specified by the  attribute), in url encoded format (  etc...)  These attributes are easily accessed in PHP by using the   array.

If the POST method is specified, then the data will be put into a special server variable, and accessed in PHP by using the built-in  array.

Note that in the HTML code, the  attribute of the INPUT tags are used as the reference strings in the   and   arrays, thus it is very important to always use unique values for the   attribute.

GET
To retrieve  variables in the address url, we use the following method:

Please note that  is not good to use for this example, because you will generally not want username and password details being displayed in the URL. Rather use POST for login details and authentication.

POST
To retrieve  variables, we use the following method:

Note that when  is used, the posted data is completely invisible to the user.