Fundamentals of PHP - Part 2

July 20th, 2008 | by Justin Daigle |

This is the second tutorial in the Fundamentals of PHP series, this tutorial will cover POST/GET variables.

Part 1 - Introduction to user submitted variables

If your reading this tutorial, your probably not familiar with POST/GET variables. Operating on this assumption, POST/GET variables are variables passed along from your browser to the web server. POST variables are passed on from a form, and GET variables are passed on inside the URL. (Note: Forms can submit both POST, and GET style variables. However you cannot place POST variables in the URL)

Part 2 - A practical example of GET variables

We’ll start with GET variables first. If you’ve ever looked at a URL on a dynamic website, you may notice it contains some of the info you submit/relevant information to what you are viewing.

Take a look at the URL for the search term “LogicDeck” on google:

http://www.google.ca/search?hl=en&q=LogicDeck&btnG=Google+Search&meta=

You may notice that after the /search you see a question mark. GET variables are passed on always after a question mark, this is very important because it tells the web server that you are done passing on URL information, and will now pass on variables.

This particular URL contains four variables, I will remove everything prior to the ? and change the &’s into new lines, this should make it more readable.

hl=en
q=LogicDeck
btnG=Google+Search
meta=

If we wanted to access the “hl” variable from within PHP, we need to use the array $_GET. The $_GET array contains all the $_GET variables posted to the URL, in this case it would have four values. $_GET['hl'], $_GET['q'], $_GET['btnG'], and $_GET['meta']

Part 3 - Applying our knowledge of GET variables

If you took the previous PHP tutorial, you will know that working with these variables is really quite simple, the following code will display the value of an $_GET variable.

Save this file as “test-get.php”

<?php
//Echo the URL variable uvar
echo($_GET['uvar']);
?>

Then navigate to your page like so (replacing ‘domain’ with your full web-accessible directory path):

http://www.example.com/test-get.php?uvar=Test+123

You should see the output “Test 123″, try changing the “Test+123″ part of the URL to change your results.

Part 4 - How POST variables work

Generally when you are submitting login information, you submit it in the form of a POST variable. POST variables cannot be seen by the user, unlike GET they are not sent in the URL, they are sent behind the scenes.

If you want to see an example of a POST form in usage go to the GMail login page www.gmail.com - where you enter your username and password, those field are submitted as POST variables.

Part 5 - Setting up an example form

You probably will want to test how the POST variable system works, as it’s very useful for logins, and other form submissions. What I’ll be showing you is a two part process, page one will be the HTML form, and page two will be the PHP that displays what was sent with the form, these concepts can easily be applied to such things as logins, and contact forms.

Save this as post-form.html

<html>
<head>
<title>LogicDECK - Post Example</title>
</head>
<body>
<form action="post-receiver.php" method="post">
<input type="text" id="data" name="data" value="Insert some text here"> <input type="submit" id="submit" name="submit" value="Send to receiver.php!">
</form>
</body>
</html>

Save this as receiver.php

<?php
//Set the value of the data variable
$data = $_POST['data'];
//Return the data variable to the screen
echo($data);
?>

If you now navigate to http://yourserver/post-form.html and submit data in the form - you should see it echoed back to you when you submit the form. The same principle can be applied to login forms, contact forms, and pretty much anything else that takes input.

This tutorial is #2 in the Fundamentals of PHP tutorial series by Justin Daigle. In the next tutorial I will be going over Arrays, Loops, and some other cool functions used for retrieving data from remote servers.

You must be logged in to post a comment.