Sunday, December 27, 2009

PHP: Send information through URL

This lesson deals with a very important part of PHP and that is using the URL to send information between pages. I will be using very basic examples to show how things work. But it is up to you how well you can use these things for advance use.

To start with, there are different ways to pass information between pages. One very crude method will be to send the information through URL. For example you may have seen a lot of URL’s like this: http://www.domainname.com/index.php?id=1&name=xyz

This is what we call sending information through the URL. In the above example, two separate values are being passed, i.e. the “id” with a value of 1 and also the “name” with a value xyz.

We can fetch these values in another page and then use them for anything. So, to do that, let’s look at some code.

The plan is to make a link on one page which will pass the information to the second page. We will not be using a very complicated HTML code, so anyone can understand this.

So, the name of my first page is "tutorialPage01.php" and here is the code for the same:

<?php
echo '<a href="tutorialPage02.php?id=1&username=John">Click here to pass the information to the next page</a>';
?>

This is where I am sending the information with different variables. Now, we will set up the next page which is “tutorialPage02.php”. This is the page which we have linked to in the link tag. This page will take the information from the URL and will display the same on the page.

<?php
echo $_GET ['id'];
echo "<br />";
echo $_GET ['username'];
?>

Here we are doing very simple things. As the variable's values are already in the URL which is passed, we are directly displaying the values of the same on the page. First is the printing of the values stored in ‘id’, then we used a break operator to go to the next line and then we are printing the value stored in 'username'.

1 comment:

  1. PHP Development Service assists organizations to deploy their collaboration, resource planning, customer and supplier management systems as web applications.
    PHP Developers

    ReplyDelete