Saturday, October 14, 2017

PHP Form Handling

skptricks PHP Form Handling

This tutorial we discuss about PHP form handling. PHP Forms are used in most of web applications and you can say it is a most integral part of web application. PHP Forms are used to send and receive data request depending upon requested form details. Here we learn about POST and GET request of PHP Form.

PHP has two superglobals global function to get PHP form data, which are as follows :
  1. $_GET
  2. $_POST 

PHP Get Form

when we are not providing any method type in form tag then Get request is consider as default form request. The data passed through get request is visible on the browser URL. You can send limited amount of data through get request. Get Request is consider as less secure one because all the requested data is visible in browser URL.

we have to specify the below code, to set form as GET Request.
<form action="request.php" method="get">

Let's see a simple example to receive data from get request in PHP.
index.php
<form action="request.php" method="get">  
Username: <input type="text" name="name"/> 
Mobile: <input type="text" name="mobile"/ 
<input type="submit" value="submit"/>  
</form>

request.php
<?php  
$username=$_GET["name"];// get the value for username
$mobile=$_GET["mobile"];// get the value for mobile
echo "Welcome, $name  "."your mobile no is: $mobile";  
?>

when you submit the form request below details are appeared on the browser page.

PHP 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.
Post Request has advantage over Get Request:
The data passed through post request is not visible on the URL browser so it is secured. Also You can send large amount of data through post request.

Let's see a simple example to receive data from post request in PHP.

index.php
<form action="request.php" method="post">  
Username: <input type="text" name="name"/> 
Mobile: <input type="text" name="mobile"/ 
<input type="submit" value="submit"/>  
</form>

request.php
<?php  
$username=$_GET["name"];// get the value for username
$mobile=$_GET["mobile"];// get the value for mobile
echo "Welcome, $name  "."your mobile no is: $mobile";  
?>

Note : When you submit the above post request, then requested form data will not appear on the browser URL. Always try to use post request because it is more secure one.



No comments:

Post a Comment