If you took the liberty of reading the tutorial on Variables (Located Here), then you should find this tutorial very interesting.
Have you ever wondered how data can be passed from one page to another without the use of databases - well, GET and POST variables is the way to do it.
There are two types for a reason and both are explained below:
GET
GET variables pass data from page to page by inserting it onto the end of the URL in the address bar. For example viewforum.php?f=6.
As you can see the URL ends with a ? and then the word 'sort' followed by and equals sign and the word 'web'. What this does is it tells the browser to transfer a variable called sort across to the new page in the URL with value of 'web'.
We can add additional variables after too like so: viewforum.php?f=6&t=683
By introducing an ampersand(&) and then using the same method as before, we can transfer as many variables as we want within reason.
On the new page, we can then ask for the variables to display thier contents using the following:
- Code: Select all
//Outputs 'web'
echo $_GET["sort"];
//Outputs the number 1
echo $_GET["page"];
-Advantages-
Users can see and amend the variables in the address bar if they so wish to. It is also easier as a programmer to see what data is being transferred and whether it is correct.
It is also possible to write anchors (<a>) that link to pages with the GET variables in, just so long as the HREF property has them in.
You can go directly to a page without having to visit another page beforehand (See POST disadvantages for more info)
-Disadvantages-
It isn't very secure - as users can view the data being sent across then data such as passwords or any other sensitive data should NOT be sent using GET.
POST
Using POST is pretty similar to using GET, however the data sent across the pages is hidden and doesn't get displayed in the URL. This is far more secure as the users cannot view the data that is being sent across.
It is also impossible for the user to view the data before it is shown on the page (that is if you as a programmer allow it to be seen). This makes sending information such as passwords much safer than using GET.
On the next page, to obtain the values of the data we would do the following:
- Code: Select all
//Outputs 'web'
echo $_POST["sort"];
//Outputs the number 1
echo $_POST["page"];
-Advantages-
Users cannot see the data being transferred meaning that sensitive data may be sent across without the worry of the average Joe viewing it in the URL.
-Disadvantages-
Whereas GET URL's can be amended and constructed without having to visit a page beforehand, POST variables cannot and must be generated via the means of a form on a previous page. Only then can they be sent across to another page.
FORMS TO PRODUCE VARIABLES
As mentioned a little while ago, all POST variables must come from a form. GET variables can also come from a form if you wish but it isn't necessary.
To make a form we do the following:
- Code: Select all
<form method="POST" action="login.php">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="Login" />
</form>
What this does is make a form that deals with POST variables (although this can be set to GET - but note that only one method can be used in any one form) - it then specifies the new page to link to when the submit button is clicked (a submit button MUST be used to send the POST variables).
We then have two input boxes, one text and one password - each has a unique name 'username' and 'password'. When the page has loaded onto the next page, the POST (or GET) variable names will be the same as the name given to the element in which they were entered into.
So anything typed into the 'username' box will appear as either:
..login.php?username=dan
which we then call using $_GET["username"]
OR
$_POST["username"]
HIDDEN ELEMENTS
There is also a handy element called a Hidden Form Element. This can contain data but isn't displayed to the user and cannot be changed. This is useful for storing temporary data that may need to be passed onto more than one page but not necessarily viewed or used. To do this it is pretty much the same as a standard form element like so:
- Code: Select all
<input type="hidden" name="ref" value="100" />
This would then be sent along with anything else (providing it is re-read back into the box each time a new page loads) and can easily be used as you would any other POST variable.


