Using $_GET to pass data between PHP pages

Often you will want to pass data from one page to another. You see examples of this without realising it. When using most blogs you will see something like ?p=12 in the URL. This is telling the next PHP page to show certain information.

Create two pages. The first is getsend.php (again it is only HTML) which should have this in the BODY:

            
<p>What language should be used?</p>
<ul>
    <li><a href="getget.php?language=english">English</a></li>
    <li><a href="getget.php?language=french">French</a></li>
    <li><a href="getget.php?language=german">German</a></li>
    <li><a href="getget.php?language=spanish">Spanish</a></li>
</ul>       
        

Now getget.php:

echo "<pre>";
    print_r($_GET);
echo "</pre>";
$chosenlanguage=$_GET["language"];
echo "<p>You chose $chosenlanguage</p>";            
        

Load getsend.php in a browser and hover over the links in turn. Look in the status bar of your browser (normally at the bottom). The ? starts the data which will be sent by putting it in the URL. This is a pair of items separated by an equals sign. The first part is like a variable name and the second is the data. The second page then extracts those pairs for use.

It is also possible to send more than one piece of data:

            
<a href="getget.php?language=spanish&colour=red">spanish</a>            
        

Look at items for sale on sites such as Ebay or Amazon for how many items can be sent using $_GET and how confusing it can be.

submit to reddit Delicious Tweet