Passing Variable to the Next Page in PHP

Passing Variable in PHP

I wanted to pass a variable to the next page using PHP/MySQL and then retrieve/use it there. After a bit of searching and reading on the internet, I found a really easy way to do it and now I'll be sharing this information with you in this article.

Here is an example:
I have two pages
1) A login page (for the user to submit his/her username and password.)
and
2) A welcome page (which will be loaded in case the username and password is correct.)

The variable to be passed is '$login'. It will be assigned to another variable i.e. $id and used in the welcome page.


In this method the variable will be passed through the URL. Just add ?id=$login to the URL and you are almost done.

In my case the example would be:
header("location:welcome.php?id=$login")


Then retrieve this variable on the welcome page using "$_GET". Here is the example:

<?php
$name = $_GET['id'];
print "$name";
?>
(This code will not only get the id and save it in a new variable name but also print it.)

Now you are done and you are free to use this variable anywhere in this page in the form of &name.

More by this Author

  • Rain Quotes
    115

    Here are some interesting, inspirational and touching quotes about rain. I liked them and hope you will also like these quotable quotes. There's always a period of curious fear between the first sweet-smelling...

  • Bamboo Palm - The Beautiful Lovable Palms
    28

    The bamboo palm (chamaedorea seifrizii) is a beautiful, commonly kept houseplant that works as an air purifier. It has a bamboo like stem and a few of these stems grow from an underground bulb of roots. Leaves are flat,...

  • 1000 Interesting Facts
    66

    I have collected these random facts from various sources on the internet(like Wikipedia), so they may or may not be true. Now please proceed to reading these interesting random bits. Butterflies cannot fly if their...


Comments 4 comments

snkhan120 profile image

snkhan120 6 years ago

great work

plz check my php mysql connection

hubpages.com/hub/PHP-mySQL-connection


Ultimate Hubber profile image

Ultimate Hubber 6 years ago Author

I saw your hub and it is not a high quality and helpful one. Try to observe writing of some good hubbers here: http://hubpages.com/authors/best/


Dimitri Rytsk profile image

Dimitri Rytsk 5 years ago

How to pass misspelled or lost URL to site search (like customer Google site search) page? I have seen it somewhere. So instead of Page Not Found user will see search results relevant to request.


Ultimate Hubber profile image

Ultimate Hubber 5 years ago Author

Dimitri, I am not sure about it. However, my hosting service and almost all the major web hosting providers provide custom error pages. So, you might want to search help pages of your web hosting.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working