$_GET, $_POST, and $_REQUEST


$_GET Variable

What is it?

The $_GET variable is used to get data from a form that is written in HTML. Also in the url $_GET variable will display the data that was taken by the $_GET variable. For example using the second example on this page it will display in the url as ?name='it will equal to what text was entered in the text box'. $_GET has limits on the amount of information that can be sent.

How to use it

Before you can use the the $_GET variable you have to have a form in html that has the method equal to GET. Then in the php, you can use the $_GET variable to get the data that you wanted. The $_GET syntax is ($_GET['name of the form field goes here']).

Examples

The $_GET Syntax

<?php
//Displays the data that was received from the input box named name in the form
	($_GET['form name goes here'])
?>		     	
		

HTML form with $_GET

<?php
// It will display the data that it was received from the form called name
 	 echo ($_GET['name']);
 ?>
 
 //This is the html form that creates the input box and submit button
 //The method for the form is in the line below
 <form action="name of the php file that has the ($_GET[]) variable in it" method="GET">
 	Name:<input type="text" name="name">
<input type="submit" value="Submit"> </form>

$_POST

What is it?

The $_POST variable is also used to collect data from forms, but the $_POST is slightly different because in $_GET it displayed the data in the url and $_POST does not. The data that $_POST gets, is invisible to others and the amount that can be sent is not limited besides the 8MB max size.

How to use it?

Before you can use the the $_POST variable you have to have a form in html that has the method equal to POST. Then in the php, you can use the $_POST variable to get the data that you wanted. The $_POST syntax is ($_POST['name of the form field goes here']).

Examples

The $_POST syntax

<?php
//The $_POST gets the data from the form 
	($_POST['form name goes here'])
?>
			

The html form with $_POST

<?php
//Displays the data that was received from the input box named name in the form
 echo ($_POST['name']);
?>	

//This is the html form that creates the input box and submit button
//The method for the form is in the line below		
<form action="test.php" method=POST>
 	Name:<br><input type="text" name="name"><br>
 	<input type="submit" value="Submit">
 </form>			
			

$_REQUEST

What is it?

The $_REQUEST variable is a variable with the contents of $_GET and $_POST and $_COOKIE variables.

How to use it?

Before you can use the the $_REQUEST variable you have to have a form in html that has the method equal to GET and POST. Then in the php, you can use the $_REQUEST variable to get the data that you wanted. Depending on what you wrote for the method in the form and using $_REQUEST in the php, $_REQUEST will use $_Get if GET is written for the method and $_REQUEST will use $POST if POST is written in the method. The $_REQUEST syntax is ($_REQUEST['name of the form field goes here']).

Examples

The $_REQUEST syntax

<?php
//this will stay the same but the method in the form will change to you preference from GET or POST
	($_REQUEST['form name goes here'])
?>			
			

Using GET for the method

<?php
//Displays the data that was received from the input box named name in the form
	echo ($_REQUEST['name']);
 ?>
 
 //This is the html form that creates the input box and submit button
 //The method for the form is in the line below
 <form action="test.php" method=GET>
 	Name:<br><input type="text" name="name"><br>
 	<input type="submit" value="Submit">
 </form>
 			

Using POST for the method

<?php
//Displays the data that was received from the input box named name in the form
	echo ($_REQUEST['name']);
 ?>
 
 //This is the html form that creates the input box and submit button
 //The method for the form is in the line below
 <form action="test.php" method=POST>
 	Name:<br><input type="text" name="name"><br>
 	<input type="submit" value="Submit">
 </form>
 			

This page was created by Nilesh Contractor