articles

Home / DeveloperSection / Articles / Form Handling in PHP

Form Handling in PHP

Samuel Fernandes 2608 06-Jun-2016

In previous article we discussed about loop in php. Now we discuss about form handling in php

GET Method

 Information sent by the user are visible to everyone (all name and values are displayed in the URL). So, GET method cannot be used to send sensitive information (like password).Get also has limitation on the amount of information to be send. The limitation is about 2000 characters. Get method is used to bookmark the pages.

Note: Get method should never be used to send password or other sensitive information. Here is an example to show how to use GET method:

Step 1: create a form (form.php)  

// action is used to send the information on getmethod.php

<form method="get" action="getmethod.php">//As we have used get method in the form, so we have to use $_GET or $_REQUEST but not $_POST (mandatory). Name:<input type="text" name="name">
Age:<input type="number" name="age">
<input type="submit" name="submit">
</form>

Step 2: getmethod.php.

<?php
if(isset($_GET["submit"])) //$_GET is a superglobals variable used to receive sent information at server end. {
$name=$_GET["name"];
$age=$_GET["age"];
echo "Name of student: $name. </br>";
echo "Age of student: $age";
}
?>

  Form Handling in PHP

POST Method: 

Unlike GET method, information sent by the user is hidden in case of POST method. Developers mainly used POST method.POST method does not have limitation on amount of information to send. As variables are not shown in URL so it is not possible to bookmark the page.

NOTE: To send sensitive information use only POST method:

Step 1: create a form (form.php)

<form method="post" action="postmethod.php">//As we have used post method in the form, so we have    to use $_POST or $_REQUEST but not $_GET (mandatory).
Name:<input type="text" name="name">
Age:<input type="number" name="age">
<input type="submit" name="submit">
</form>

Step 2: Create postmethod.php

<?php

if(isset($_POST["submit"]))"])) //$_POST is a superglobals variable used to receive sent information at server end. {
$name=$_POST["name"];
$age=$_POST["age"];
echo "Name of student: $name. </br>";
echo "Age of student: $age";
}
?>

 

 Form Handling in PHP

NOTE: $_REQUEST is an associative array that by default contains the content of $_POST, $_GET and $_COOKIE which is used to receive information whether the method is POST or GET.

Step 1: Create a form. (form.php)

//Form is created

<form method="post" action="phpintro.php">
Name:<input type="text" name="name">
Age:<input type="number" name="age">
<input type="submit" name="submit">
</form>

 Step 2: Receive information using $_REQUEST:

<?php
if(isset($_REQUEST["submit"])) // $_REQUEST used to receive information. But we cannot use $_GET {
$name=$_REQUEST["name"];
$age=$_REQUEST["age"];
echo "Name of student: $name. </br>";
echo "Age of student: $age";
}
?>

 

Similarly, in case of get method we can use $_REQUEST method to receive the information.

Step 1: Create a form.

//Form is created
<form method="get" action="phpintro.php">
Name:<input type="text" name="name">
Age:<input type="number" name="age">
<input type="submit" name="submit">
</form>

 Step 2: Receive information using $_REQUEST:

<?php
if(isset($_REQUEST["submit"])) // $_REQUEST used to receive information. But we cannot use $_POST {
$name=$_REQUEST["name"];
$age=$_REQUEST["age"];
echo "Name of student: $name. </br>";
echo "Age of student: $age";
}
?>

Updated 07-Sep-2019

Leave Comment

Comments

Liked By