In this lesson, you will learn about Form Handling in PHP, its usage, and examples to better understand the topic.
Users who log in to a website or email inbox interact with a form. A form contains graphical user interface items such as input boxes, radio buttons, checkboxes, etc. The Forms are required to get input and submit it for processing to the web server. The diagram below shows how to handle the Form.
Forms are helpful when developing flexible and dynamic user input applications. You can use a Form to create dynamic websites which contain client-side or server-side scripting or both. These websites generate the changing content using either one programming language or a combination of multiple programming languages and scripting languages. Dynamic websites provide features to store, update, retrieve and delete data from a database.
A form starts and ends with a form tag in HTML: opening and closing form tags <form>
and </form>
respectively. The form tag encompasses all the inputs and provides directions on how and where to submit the Form. Let’s begin, for instance, by creating a form in a file called simpleForm.php.
<form action=" simpleForm.php" method="post"> <!-- code --> </form>
The “action indicates the page where the form is submitted.” The form and the action will often be on the same page, but this is not a rule.
The “method” shows how the Form will behave. The Form’s action attribute indicates the data processing submission URL. The attribute of the method indicates the sort of submission. Most of the time, forms use the “post” method to get data. Two PHP superglobals, $GET
and $POST
, are used for form-data collection.
$POST
and $GET
are the built-in PHP super global array variables used to get values submitted. The array is created by both GET and POST, e.g. array(key1= > value1)
. This array contains key/value pairs, where keys are from control names and values are user input data. GET and POST are written as $_GET
and $_POST
. These are superglobals, meaning they are accessible from any script in the program; it has a global scope.
They are always available, irrespective of their range, and you can access them from any function or class.
The POST method is ideal if you don’t want to show the form post values in the URL. When submitting login details to the server, a good example is using the post method.
A built-in PHP super global array variable with the global scope is used to get values submitted via the HTTP GET method. It shows the form get values in the URL.
<?php $_GET['var']; ?>
Explanation
$_GET[…]
is the PHP array
'var'
is the URL variable name.
<HTML> <body> <form action="simpleForm.php" method="get"> <h3>What is your favorite programming Language? </h3> Please Enter: <input type="text" name="name"><br> Press Button :<input type="submit" name="submitPLanguageName" value="submit"> </form> </body>
PHP Page simpleForm.php receives the HTML from values
<?php if ($_GET["submitPLanguageName"] == "submit") { $favLang = $_GET["name"]; echo "My Favourite Movie is ", $favLang; } ?>
The variable $REQUEST
contains the variables $GET
and $POST
, and $COOKIE
. You must have a form in HTML that has the method equivalent to ‘GET’ and ‘POST’ before you can use the $REQUEST
variable. You can then use the $REQUEST
variable in PHP to get the information you want.
Depending on what you write in the Form for the procedure and using $REQUEST
in the PHP, $REQUEST
will use $Get
when GET is written for the procedure, and $REQUEST
will use $POST
when POST is written.
<?php ($_REQUEST['varname']) ?>
Using the POST method for posting values and REQUEST to receive
//An HTML form that produces the input box and submit button <HTML> <body> <form action="simpleForm.php" method="get"> <h3>What is your favorite programming Language?</h3> Please Enter : <input type="text" name="name"><br> Press Button :<input type="submit" name="submitPLanguageName" value="submit"> </form> </body>
PHP Page simpleForm.php receives the HTML form values
<?php if ($_REQUEST["submitPLanguageName"] == "submit") { $favLang = $_REQUEST["name"]; echo "My Favourite Movie is ", $favLang; } ?>
Using the POST method for posting values and REQUEST to receive
//An HTML form that produces the input box and submit button <HTML> <body> <form action="simpleForm.php" method="post"> <h3>What is your favorite programming Language? </h3> Please Enter: <input type="text" name="name"><br> Press Button :<input type="submit" name="submitPLanguageName" value="submit"> </form> </body> PHP Page simpleForm.php receives the HTML from values: <?php if ($_REQUEST["submitPLanguageName"] == "submit") { $favLang = $_REQUEST["name"]; echo "My Favourite Movie is ", $favLang; } ?>
A built-in PHP super global array variable with the global scope is used to get values submitted via the HTTP POST method. It doesn’t show the form values in the URL.
Basic Syntax
<?php $_POST['var']; ?>
Explanation
$_POST[…]
is the PHP array
'var'
is the URL variable name.
Simple HTML Form using POST method Example
<HTML> <body> <form action="simpleForm.php" method="post"> <h3>What is your favorite programming Language? </h3> Please Enter: <input type="text" name="name"><br> Press Button :<input type="submit" name="submitPLanguageName" value="submit"> </form> </body>
PHP Page simpleForm.php receives the HTML from values
<?php if ($_POST["submitPLanguageName"] == "submit") { $favMovie = $_POST["name"]; echo "My Favorite Movie is ", $favLang; } ?>
First, consider the above example of the Form to understand the flow. HTML form attribute action = simpleForm.php
means that the form variables send to the ‘simpleForm.php’ page, and the page will send values through the browser using method=POST
. So, we first need to check and verify if a form has been submitted. Let’s see the value of the submit button named submitPLanguageName
to do that. The value of submit button on the HTML form is submitted. We put the condition on the simpleForm.php page.
If the submit button value $_POST['submitPLanguageName']
is ‘submit’, then a form has been submitted. If not, the user will probably visit this page for the first time.
To access the value of the submitted data, use the $_POST['name']
array, where “name” is the name in the HTML input element. We practice $_POST
since that is the Form’s method. If it were “get” in its place, we would use $_GET[]
. After we have checked the “Form” is submitted, let’s get the text that the user typed will be displayed on the next page using PHP code. Again, we use the $_POST
array to get the $favLang
variable and print it using echo.
Last, we’ve added a “value” field to the input box and written PHP code to set the value to whatever the user provided. The text you entered will still be visible on the page if you submit the Form.
POST | GET |
---|---|
$POST is an array of variables passed through the URL parameters to the present script. | $GET is an array of variables passed through the URL parameters to the present script. |
Information sent from a form using the POST method is invisible to others (all names/values are embedded in the HTTP request body). | Everyone can see information sent from a form using the GET method (all variable names and values are shown in the URL). |
There are no limitations on how much information to send. POST also promotes sophisticated features such as multipart binary input assistance while uploading files to the server. However, it is not feasible to bookmark the page because the variables are not presented in the URL. Developers prefer to send information from the Form using POST. | The data sent by “GET” also has boundaries. The restriction is approximately 2000 characters. However, it is feasible to bookmark the page because the variables are presented in the URL. In some instances, this may be helpful. Use GET to send non-sensitive information. Don’t use GET to transmit passwords or other sensitive information. |
Time spent encapsulating the PHP POST values in the HTTP body has reduced efficiency compared to the GET technique. | Has high efficiency in comparison with the POST method dues to the concise nature of attaching the URL values. |
Supports many diverse types of data like string, numeric, binary, etc. | Supports only string data types because the values are shown in the URL. |
This concludes the PHP Form Handling lesson. In the next lesson, you will learn about PHP Form Validation.