Handling HTML Forms
HTML form is used to take the user input in text field, textarea, radio and other input fields. User submits the HTML form to the target page on the server. HTML Form is used for the development of dynamic web applications where the user enters the input and based on the input, the server sends a response to the user.
Form is defined inside <form> </form> tags. Sometimes the server needs to store the input data in a database submitted by the user. To edit the already submitted data, the user may need the form again. Refer form elements for more.
Submitting data to the server
<h2><u>Student Registration Form</u></h2>
<form method="post" action="submit.php">
<label>First Name</label>
<input type="text" name="first_name">
<br><br>
<label>Last Name</label>
<input type="text" name="last_name">
<br><br>
<label>Date of Birth</label>
<input type="text" name="dob">
<br><br>
<label>Gender</label>
<input type="radio" name="gender" value="male"> Male <input type="radio" name="gender" value="female"> Female
<br><br>
<label>Address</label>
<textarea name="address"></textarea>
<br><br>
<input type="submit" value="submit"> <input type="reset" value="reset">
</form>
The form will look like
Student Registration Form
Once the submit button is clicked, form submits to the page on the server specified in the action attribute. In the form, notice the 'name' attribute which is used to identify the values passed by an input element on the server side.
Passing information between pages
As you know, HTTP is a stateless protocol. HTTP requests to the server are independent of each other. Each request initiates a discrete process, which serves up a single file and then ends automatically.
So it is impossible to pass information between two pages unless we employ some extra means to pass the information from one file to another page or program. That is why we need PHP form handling techniques. PHP can catch the variable passed from one page to the next and make it available for further use.
PHP can pass the data between pages using session variables, cookies, URLs and forms. Using AJAX, PHP can communicate with the javascript.
There are two ways the browser can send information to the web server:
- The GET Method
- The POST Method
The GET method sends the data appended to the page request or page URL. URL and the page name are separated using the ‘?’ character.
In the POST method, the data is sent to the server as a package in HTTP headers. Data sent through the POST method will not be visible in the URL. Super global variables are built-in variables that are always available in all scopes.
$_GET
PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with the method 'get'. $_GET can also collect data sent in the URL. $_GET is not stored on the server, but is passed with each HTTP request.
In 'action.php' page
<a href="action.php?id=5&colour=green">Item Colour</a>
<?php
echo $_GET['id'];
echo $_GET['colour'];
?>
When a user clicks on the link ‘Item Colour’, parameters id and colour are sent to action.php. Values are received at the server side page action.php using $_GET.
$_POST
In the post method, data is passed in the HTTP headers rather than simply appended to the end of the URL. $_POST is a PHP super global variable which is used to receive form data after submitting an HTML form with the method 'post'.
In 'action.php' page
<form method="post" action="action.php">
<label>ID</label>
<input type="text" name="id" value="5">
<br><br>
<label>Colour</label>
<input type="text" name="colour" value="orange">
<br><br>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["id"]))
{
echo $_POST['id'];
echo $_POST['colour'];
}
?>
When a user clicks on the submit button, id and colour are sent to 'action.php'. Values are received at the server side page 'action.php' using $_POST.
Comparing GET and POST methods
GET | POST |
---|---|
We can not send a large amount of data because the request parameter is appended into the URL. | Large amount of data can be sent because the request parameter is appended into the body. |
GET request is less secure as the data is exposed in the URL bar. | Data passed using the POST method will not be visible in query parameters in browser URL. |
In the GET method only ASCII characters are allowed. | In the POST method all types of data are allowed. |
Get requests can be bookmarked. | Post requests cannot be bookmarked. |
Parameters remain in browser history because they are part of the URL. | Parameters are not saved in browser history. |
GET is more efficient | POST is less efficient compared to GET |
PHP Other Super Global Variables
$_REQUEST
PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. $_REQUEST by default contains the contents of $_GET, $_POST and $_COOKIE. It means, we can receive the data passed using get and post methods.
Eg:
In action.php
<form method="post" action="action.php">
<label>ID</label>
<input type="text" name="id" value="5">
<br><br>
<label>Colour</label>
<input type="text" name="colour" value="orange">
<br><br>
<input type="submit" value="submit">
</form>
<?php
echo $_REQUEST['id'];
echo $_REQUEST['colour'];
?>
$GLOBALS
$GLOBALS is used to access global variables from anywhere in the PHP script. PHP stores all global variables in an array called $GLOBALS['index']. The index contains the name of the variable.
$_SERVER
$_SERVER holds information about headers, paths, and script locations. Eg: $_SERVER['SERVER_NAME'] gives the value of the server name in host configuration.
$_FILES
$_FILES is a super global variable which can be used to upload files. Ensure that the attribute enctype is set to "multipart/form-data" in the HTML form if it contains <input type="file">.
$_ENV
$_ENV is used to return the environment variables from the web server. These variables are imported into PHP's global namespace from the environment under which the PHP parser is running.