Jump to content

  •  

Photo
- - - - -

P10: Passing Variables P2 GET

PHP4Beginners

  • Please log in to reply
No replies to this topic

#1 DraxxTV

DraxxTV

    Member

  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 10:14 PM

GET

The GET method allows the user to pass variables through a url. Information sent this way is visible to the user and should not be used to send private information such as usernames and passwords. There is also a limit on the amount of data which can be sent this way and should only be used if data is smaller than 2000 characters.

 

Creating a Form

The first thing you need to do to get started is to create a HTML form on a web page. for the purpose of this tutorial the all of the code will run together on the same page instead of using multiple pages. I am assuming you understand basic HTML so I will  not be explaining everything you may need to know about HTML to use this tutorial. In the following form, I will be creating a demo page with a few peoples profile pictures. When you click on that profile picture, it will bring you to a page specifically for that person.

<html>
    <head>
        <title>PHP For Beginners P10: Passing Variables P2 GET</title>
    </head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P2 GET</h1>
            <table>
                <tr>
                    <td><a href="profile.php?user=1"><img src="images/billgates.png"></a></td>
                    <td><a href="profile.php?user=2"><img src="images/emmawatson.png"></a></td>
                    <td><a href="profile.php?user=3"><img src="images/mattsmith.png"></a></td>
                    <td><a href="profile.php?user=4"><img src="images/robertdjr.png"></a></td>
                    <td><a href="profile.php?user=5"><img src="images/scarlettjohansson.png"></a></td>
                </tr>
                <tr>
                    <td><a href="profile.php?user=1">Bill Gates</a></td>
                    <td><a href="profile.php?user=2">Emma Watson</a></td>
                    <td><a href="profile.php?user=3">Matt Smith</a></td>
                    <td><a href="profile.php?user=4">Robert Downey, Jr.</a></td>
                    <td><a href="profile.php?user=5">Scarlett Johansson</a></td>
                </tr>
            </table>    

        </center>
    </body>
</html>

Where there is a picture, there is a hyperlink to the profile.php page. After the profile.php page, you will see there is a question mark and then user=#. Here we are passing a variable through the url saying that the user equals a certain number. From this, the next profile page will be able to see what number it is and show the user an appropriate profile for that person. The way this works is with using the GET method.

The next page will use arrays to store information about each of the people and using the GET method, it will show the appropriate information for that person. The arrays are as displayed as follows. You should understand this. If not, check out our guide about arrays:

$firstNameArray=array(
    1 => "Bill",
    2 => "Emma",
    3 => "Matt",
    4 => "Robert",
    5 => "Scarlett");
$lastNameArray=array(
    1 => "Gates",
    2 => "Watson",
    3 => "Smith",
    4 => "Downey Jr",
    5 => "Johansson");                    
$ageArray=array(
    1 => 57,
    2 => 23,
    3 => 30,
    4 => 48,
    5 => 28);
$nationalityArray=array(
    1 => "English",
    2 => "English",
    3 => "English",
    4 => "American",
    5 => "American");
$websitesArray=array(
    1 => "http://www.thegatesnotes.com/",
    2 => "http://www.emmawatson.com/",
    3 => "n/a",
    4 => "http://robert-downeyjr.net/",
    5 => "http://scarlettjohansson.com/");
$profilePictureArray=array(
    1 => "/images/billgates.png",
    2 => "/images/emmawatson.png",
    3 => "/images/mattsmith.png",
    4 => "/images/robertdjr.png",
    5 => "/images/scarlettjohansson.png");

In the next part we are just going to get the which user has been selected using the GET method. As mentioned above, each variable has been passed in the format of ?variable=value. Multiple of these can be stung together using the & symbol. This would look like this ?firstvariable=value&secondvariable=value. As we have passed the id of the user through the address bar like this: ?user=id, to use this in the profile page, we simply add this

$user=$_GET['user'];

To stop people tampering with the url, we are going to add a few more lines of code.

if(isset($_GET['user'])){
    $user=$_GET['user'];
}else{
    $user=1;
}

This just checks that the variable has been set in the url and if it hasnt then we are just declaring it as 1. This is just so it shows a users data. As we only have 5 users, if the  user was to add a random high number which would not exist like 100, this would also make it display a blank page. What we could do to stop this is to count the values in the array and say that if the user variable is higher then the maximum amount of values in the array then to just display the highest instead. We can then display a message to the user telling them what has happened.

$numberOfUsers=count($firstNameArray);
if($user>$numberOfUsers){
    $user=$numberOfUsers;
    echo "<center><p style='color:red'>We cannot find the user you are looking for but here is another user instead</p></center>";
}

So, if you was to go to profile.php?user=100 then you would see the profile of Scarlett Johansson and see the following error message:

We cannot find the user you are looking for but here is another user instead

The section below will now dynamically show the information for each user. We do this by selecting the value in the array where the key matches the id of the user. For example: $firstNameArray[$user].

<html>
    <head>
        <title>PHP For Beginners P10: Passing Variables P2 GET --The Profile</title>
    </head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P2 GET --The Profile</h1>
            <table>
                <tr>
                    <td rowspan="5">
                        <img src="<? echo $profilePictureArray[$user] ?>">
                    </td>
                </tr>        
                <tr>
                    <td>Name: </td>
                    <td><? echo $firstNameArray[$user] . " " . $lastNameArray[$user];?></td>
                </tr>
                <tr>
                    <td>Age: </td>
                    <td><? echo $ageArray[$user] . " Years Old"; ?></td>
                </tr>
                <tr>
                    <td>Nationality: </td>
                    <td><? echo $nationalityArray[$user]; ?></td>
                </tr>
                <tr>
                    <td>Website: </td>
                    <td><? echo "<a href=".$websitesArray[$user].">".$websitesArray[$user]."</a>"; ?></td>
                </tr>
            </table>    

        </center>
    </body>
</html>

The full code for this page is below:

<?
$firstNameArray=array(
    1 => "Bill",
    2 => "Emma",
    3 => "Matt",
    4 => "Robert",
    5 => "Scarlett");
$lastNameArray=array(
    1 => "Gates",
    2 => "Watson",
    3 => "Smith",
    4 => "Downey Jr",
    5 => "Johansson");                    
$ageArray=array(
    1 => 57,
    2 => 23,
    3 => 30,
    4 => 48,
    5 => 28);
$nationalityArray=array(
    1 => "English",
    2 => "English",
    3 => "English",
    4 => "American",
    5 => "American");
$websitesArray=array(
    1 => "http://www.thegatesnotes.com/",
    2 => "http://www.emmawatson.com/",
    3 => "n/a",
    4 => "http://robert-downeyjr.net/",
    5 => "http://scarlettjohansson.com/");
$profilePictureArray=array(
    1 => "/images/billgates.png",
    2 => "/images/emmawatson.png",
    3 => "/images/mattsmith.png",
    4 => "/images/robertdjr.png",
    5 => "/images/scarlettjohansson.png");
if(isset($_GET['user'])){
    $user=$_GET['user'];
}else{
    $user=1;
}
$numberOfUsers=count($firstNameArray);
if($user>$numberOfUsers){
    $user=$numberOfUsers;
    echo "<center><p style='color:red'>We cannot find the user you are looking for but here is another user instead</p></center>";
}
?>

<html>
    <head>
        <title>PHP For Beginners P10: Passing Variables P2 GET --The Profile</title>
    </head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P2 GET --The Profile</h1>
            <table>
                <tr>
                    <td rowspan="5">
                        <img  width="125" height="125" src="<? echo $profilePictureArray[$user] ?>">
                    </td>
                </tr>        
                <tr>
                    <td>Name: </td>
                    <td><? echo $firstNameArray[$user] . " " . $lastNameArray[$user];?></td>
                </tr>
                <tr>
                    <td>Age: </td>
                    <td><? echo $ageArray[$user] . " Years Old"; ?></td>
                </tr>
                <tr>
                    <td>Nationality: </td>
                    <td><? echo $nationalityArray[$user]; ?></td>
                </tr>
                <tr>
                    <td>Website: </td>
                    <td><? echo "<a href=".$websitesArray[$user].">".$websitesArray[$user]."</a>"; ?></td>
                </tr>
            </table>    

        </center>
    </body>
</html>

You should be able to see that there are hundreds if not thousands of different usages for this. We will continue to use this in later tutorials and instead of having to enter our own arrays, we will instead gather information directly from a database.

Attached Files







Also tagged with one or more of these keywords: PHP4Beginners