PHP is widely used today as server side scripting language. It is mostly used to develop interactive web sites and can easily be embedded in HTML. The interaction between user/browser and the server is carried out using the HTML forms. One of the most powerful features of PHP is the way it handles HTML forms. This tutorial explain using HTML form fields like Input, Hidden, Radio Buttons, Check boxes etc. The basic concept that is important to understand is that any form field values in a form will automatically be available to your PHP scripts. HTML forms can be submitted to the handling PHP script using either GET or POST method.
See example below:
When we submit above form autoglobal $_POST['first_name'] and $_POST['last_name'] automatically available to the form handling PHP script. Similarly if GET method is used then $_GET['first_name'] and $_GET['last_name'] is available. Following example shows how we can access values in handling PHP script i.e. in action.php
There are various types of input elements are used in HTML form. Following is the list of such elements used to accept inputs from user/broswer that needs to be sent to the web server for further processing:
When we submit above form autoglobal $_POST['first_name'] and $_POST['last_name'] automatically available to the form handling PHP script. Similarly if GET method is used then $_GET['first_name'] and $_GET['last_name'] is available. Following example shows how we can access values in handling PHP script i.e. in action.php
There are various types of input elements are used in HTML form. Following is the list of such elements used to accept inputs from user/broswer that needs to be sent to the web server for further processing:
Text input, Hidden input and Password type input fields
This input form element can be used for a single lime text input from the user. For example user first name, user e-mail, phone numbers etc. The syntax in HTML form is as below
This will display an input box in the browser and the values of this element canbe accessed at the form handling php script by following.
Password type The defulat type of HTML input element is text. The special password type of input can be used as following
and can be accessed in PHP similar to above input element.
Hidden type The hidden field is used to send information from browser to the server without having to input it. Typically this is used to send some logical data that has nothing to do with user but used at the server in PHP program logic. For example state, action, or passing the result to the other module etc. Please note that you encode the value using htmlspecialchars() function.
and the value of this field can be accessed in PHP by following.
This will display an input box in the browser and the values of this element canbe accessed at the form handling php script by following.
Password type The defulat type of HTML input element is text. The special password type of input can be used as following
and can be accessed in PHP similar to above input element.
Hidden type The hidden field is used to send information from browser to the server without having to input it. Typically this is used to send some logical data that has nothing to do with user but used at the server in PHP program logic. For example state, action, or passing the result to the other module etc. Please note that you encode the value using htmlspecialchars() function.
and the value of this field can be accessed in PHP by following.
Textarea multiline text input HTML form element in PHP
This form element can be used for a multi-line text input from the user. For example product description. The syntax is as below.
This will display a multi-line input area in the browser and the size can be controlled using rows and cols tag. The text value input by user can be accessed in PHP by following
This will display a multi-line input area in the browser and the size can be controlled using rows and cols tag. The text value input by user can be accessed in PHP by following
Select (Pull Down menu) and Select multiple options in PHP
This type of HTML form element is used to allow user to select from multiple choices. For example selecting a month or a day. The html syntax is as below.
The value of selected choice can be accessed in form handling PHP script as following
The variation to this allows user to select multiple choices from the given options. This can be done by following select multiple syntax
The size tag can be used to control how many rows should be visible. And note the use of '[]' following the name of the select box. This will denote that it is an array and the choices can be accessed in form handling PHP script by following.
$value) {
echo "Key: $key; Value: $value
"; } ?> If Red and Green is selected then above code will print 0: Red 1: Green
The value of selected choice can be accessed in form handling PHP script as following
The variation to this allows user to select multiple choices from the given options. This can be done by following select multiple syntax
The size tag can be used to control how many rows should be visible. And note the use of '[]' following the name of the select box. This will denote that it is an array and the choices can be accessed in form handling PHP script by following.
"; } ?> If Red and Green is selected then above code will print 0: Red 1: Green
Radio buttons HTML form element
Radio button in HTML form is an alternative method that allows user to select from given options. Unlike drop down menu this is used to create more visibilty in the input form.
Red
Orange
Blue
The value of the selection can be accessed in PHP as following
The value of the selection can be accessed in PHP as following
Check Boxes HTML form element
This is an alternative way of allowing user to select multiple choices from the given options. Following example show the use of Check Boxes in an HTML form
Red
Orange
Blue
Violet
Black
Again here PHP array is used to access the selected choices from the user.
$value) {
echo "Key: $key; Value: $value
"; } ?> If Red and Violet is selected then above code will display 0: Red 1: Violet
Again here PHP array is used to access the selected choices from the user.
"; } ?> If Red and Violet is selected then above code will display 0: Red 1: Violet