Form Handling
Form Handling
We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET,
$_POST and $_REQUEST.
The form request may be get or post. To retrieve data from get request, we need to use $_GET,
for post request $_POST.
$_REQUEST is a superglobal array that is used to collect data from HTML forms submitted
using either the GET and POST methods.
Get Form
Get request is the default form request. The data passed through get request is visible on the
URL browser so it is not secured. You can send limited amount of data through get request.
Example:
<html>
<body>
<form method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
Output:
URL: localhost/Anu/?name=CSE&email=CSE%40gmail.com
Post Form
Post request is widely used to submit form that have large amount of data such as file upload,
image upload, login form, registration form etc.
The data passed through post request is not visible on the URL browser so it is secured. You
can send large amount of data through post request.
Example:
<html>
<body>
<form method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Output:
URL: localhost/Anu/
$_REQUEST
$_REQUEST is a superglobal array that is used to collect data from HTML forms submitted
using either the GET and POST methods.
Example:
<html>
<body>
<form method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
Welcome <?php echo $_REQUEST["name"]; ?><br>
Your email address is: <?php echo $_REQUEST["email"]; ?>
</body>
</html>
Output:
URL: localhost/Anu/?name=CSE&email=CSE%40gmail.com
Example:
<html>
<body>
<form method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
Welcome <?php echo $_ REQUEST ["name"]; ?><br>
Your email address is: <?php echo $_ REQUEST ["email"]; ?>
</body>
</html>
Output:
URL: localhost/Anu/