Php: Main - All your PHP needs!
In this tutorial, you will be learning about three superglobal arrays:
$_POST
$_GET
$_REQUEST
This will be a some-what short tutorial, but these are all very important and should be taught nonetheless. So lets begin with the $_POST suberglobal.
-----------
$_POST
The $_POST superglobal is used to access information submitted by forms via the HTTP POST method. The $_POST array is ideal to use when wanting to submit large amounts of information, because there is no limit with it. Basically, it works "inivsibly" without the user seeing what was submitted. An example is below.
A basic form:
<form action="test.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
We place the method="post" property in the <form> tag so we can let the form know we wish to send data via post. When the user clicks the submit button, they will be taken to a page like this:
somesite.com/test.php
On test.php, we can recieve the data the user submitted by using the $_POST array like this:
<?php
$fname=$_POST['fname'];
$lname=$_POST['lname'];
echo "Hello $fname $lname.";
?>
We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via post, we can access it via post.
The alternate form of $_POST is $HTTP_POST_VARS. It is like $_POST, but is not superglobal. To make it so, you must add global before it.
-----------
$_GET
The $_GET superglobal is used to access information submitted by forms via the HTTP GET method. The $_GET array is ideal to use when wanting to submit short amounts of information. Since the values are located in the url, pages can be bookmarked with the data in the url so it can be used over and over again. The get method is used by putting the data at the end of the url of a page. It is usually done by having a page like *.php?var=blah&somevar=blah. Note that you should never use thye get method when submitting sensitvie data, and that string values in the url do not need quotes. Examples of $_GET are below.
A basic form:
<form action="test.php" method="get">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
We place the method="get" property in the <form> tag so we can let the form know we wish to send data via get. When the user clicks the submit button, they will be taken to a page like this:
somesite.com/test.php?fname=somedata&lname
=somedata
On test.php, we can recieve the data the user submitted by using the $_GET array like this:
<?php
$fname=$_GET['fname'];
$lname=$_GET['lname'];
echo "Hello $fname $lname.";
?>
We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via get, we can access it via get. Also note that when separating variables in the url, use ampersands ( & ).
The alternate form of $_GET is $HTTP_GET_VARS. It is like $_GET, but is not superglobal. To make it so, you must add global before it.
-----------
$_REQUEST
The $_REQUEST method is used to retrieve data submitted via get, or post. It can also handle $_COOKIE data. I prefer not to use the $_REQUEST superglobal because the user could submit the data however he/she wants out of these three ways. But, if you wish to use it, I will explain and give examples.
A basic form:
<form action="test.php">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
Since we are using the request method, we do not need to specify how to submit the data. We can just totally get rid of the method property. When the user presses the submit button, they will be taken to a page like this:
somesite.com/test.php
On test.php, we can recieve the data the user submitted by using the $_REQUEST array like this:
<?php
$fname=$_REQUEST['fname'];
$lname=$_REQUEST['lname'];
echo "Hello $fname $lname.";
?>
We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via anyway, we can access it via $_REQUEST. So, I reccomend using either $_POST or $_GET when recieving data from forms, but use $_REQUEST if you wish.
-----------
The tutorial is now over. I hope you learned a little something today :)