by: Mark Alexander Bain
The web site designer often needs to obtain information from the web site user. For example they may need to know:
- The user name and password of the user (when logging in)
- Details about the user when registering (such as the proposed user name and password)
- Details about products when ordering on line (such as a discount voucher number)
The information gathering process consists of two stages:
- The web page presents an HTML form to the user. The form will contain input boxes, radio button and drop down lists to illicit the required information from the user
- PHP code then processes the information entered by the user. For example the code may check whether a required username already exists and creates it if it doesn’t.
The first step is, therefore, to create the form for the user data entry.
Creating a HTML Form with PHP
A form is simply the method by which the web site programmer asks the user questions and can be defined using HTML. However, the programmer will find that a more flexibly and adaptable application can be created if the form is created by using a PHP function:
In this case the user is asked for:
- Their first name and surname
- A potential user name
- A password (which must be entered twice to ensure that it is spelled correctly)
If this is saved into a PHP file then the user will be able to view the form via a web browser. It is now ready for the next stage – the processing of the input data.
Processing Data Using PHP
The programmer can write a second function to process the data entered by the user:
The purpose of this function is to ensure that the data entered by the user is valid and is then processed correctly. It does this by making use of the $_REQUEST array. The $_REQUEST array is an associative array and contains all of the data sent from the form by the user:
And the application must allow the user to correct any mistakes:
If everything has been entered correctly then the application may carry on and process the data appropriately. In this case it will add the new user to the database:
Of course, the function should only be called if the user has actually entered the information:
Now the form will only appear when the user first accesses the web page or if they then make an invalid entry. Once they submit the form then the PHP data processing will take over.
A PHP programmer uses an HTML form to obtain data from their application users. When the user submits the form then the application receives all of the data in an associative array – $_REQUEST. The application can then process the data to ensure that it is valid and is handled appropriately.