GET it from the URL
The quickest (but most limited) way to transfer variables is by a method called GET. With GET, you append the variables onto the URL of the page you want the variables to be transferred to:
http://www.matthom.com/contact.php?id=301&name=Matthom
The example above would give the contact.php page two variables to utilize: id, and name, whose values are 301, and Matthom, respectively.
You can add as many variables to the URL as you’d like.
Beware – sometimes you don’t want your variables to be shown "out in the open." Also, you are limited to 255 characters in the URL, so the variables can’t contain too much information.
From contact.php, you can GET these two variables via PHP:
GRAB THE VARIABLES FROM THE URL
$id = $_GET['id'];
$name =$_GET['name'];
POST it from a FORM
Another way to transfer variables, and by far the more robust way, is to grab them from a form.
Let’s say this is your form field code:
<form action="process.php" method="post">
<input type="text" size="25" name="searchtype" />
<input type="text" size="25" name="searchterm" />
</form>
These two input boxes allow users to enter information. At process.php, you can grab the variables in the same way:
GRAB THE VARIABLES FROM THE FORM
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];
Notice the use of $_POST[]
over $_GET[]
. This is an important distinction.