$_GET, $_POST and $_REQUEST function in PHP



$_GET, $_POST and $_REQUEST in PHP

In this php tutorial we will learn how to handle form in php. In other word we can say how to send data from one from to another and how to get that sent data in php.

There are main two methods for a form to send data in php.
The get method and post method.

we learnt in out previous php tutorial about get method and post method. both methods are used to send from data to same page or any other php script page.

In this php tutorial we learn how to get data or collecting the data passed from a form.

The $_GET, $_POST and $_REQUEST functions are used to get data from a form.

The $_GET function

The $_GET function is used to get data when we use get method (method =”get”) for send form data.
In html form we use get method to send data the data will be displayed in url. In the html form tags use method=get the in php script page we use $_GET function to retrieve data from url. The $_GET function used to retrieve data from url.

Syntax of $_GET function

$_GET[“element name”];

Example of $_GET function in PHP

First design a html web page with two textboxes and a button control. Here we use method=”get” for send form data.
We set here action=”result.php” means the data send to the result.php page. We can get data on result.php page using $_GET function.

<html>
<head>
<title>My First HTML web page</title>
</head>
<body>

<FORM name="form1" action="result.php" method="GET">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="pass"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

</body>
</html>

The html form design with two textboxes for username and password. here enter username and password then click login button to submit the form.
The form submitted with two values username and password. The data will be send on result.php page as we write action=”result.php”.
we use $_GET function to get username and password value because we use method=”get” in form tags.

The result.php page look like:

<?php

echo "Your Username = ". $_GET["name"]. "<br/>";
echo " Password = ".$_GET["pass"];
?>

In above result.php page we will retrieve the username and password values using $_GET function.

$_GET function in PHP


$_POST function in PHP

We already know the $_POST and $_GET functions used to get data from a form.
The $_POST function used when we use post method to send data.
The data or information does not seen in the address bar when use post method in php.

Syntax of $_POST function

$_POST[“element name”];

Example of $_POST function in PHP

The above same example just change the method=”post” in form tags like below:

<html>
<head>
<title>My First HTML web page</title>
</head>
<body>

<FORM name="form1" action="result.php" method="POST">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="pass"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

</body>
</html>

The result.php look like:

<?php

echo "Your Username = ". $_POST["name"]. "<br/>";
echo " Password = ".$_POST["pass"];
?>

Here above example we use $_POST function to get username and password values beacuse we use post method for send data.

$_POST function in PHP
$_POST function in PHP

$_REQUEST function in PHP.

The $_REQUEST function is used to get form data sent with both the post method and get method.

Syntax of $_REQUEST function

$_REQUEST[“element name”];

Example of $_REQUEST function in PHP

The $_Request function work with both get and post methods. In below example we use method=post for understand $_REQUEST  function.

<html>
<head>
<title>My First HTML web page</title>
</head>
<body>

<FORM name="form1" action="result.php" method="POST">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="pass"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

The result.php page we use $_REQUEST function to retrieve data from a form, sent by post method.

<?php

echo "Your Username = ". $_REQUEST["name"]. "<br/>";
echo " Password = ".$_REQUEST["pass"];
?>

$REQUEST function in PHP

 

Leave a Reply

Your email address will not be published. Required fields are marked *