Passing a parameter/variable between pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Passing a parameter/variable between pages

Post by wtc »

This is probably elementary, but here goes. I need to pass the same variable between a couple forms, and am not sure of the best way to do this using PHP. Example:

page1.php has a button that takes you to the next page. Using an <a href="...", the URL includes the id, and looks something like this: "www.mydomain.com/page2.php?my_id=2". This is working perfectly.

page2.php uses a $_GET['my_id'] to get that number 2, and does whatever it needs to do with the information. That much is also working perfectly.

page2.php also includes yet another button that will pass yet another variable to page3.php. something like: "www.mydomain.com/page3.php?another_id=4"

What I need is to have 'my_id' (from page1.php, used in page2.php) available in page3.php. I can use the $_GET to access 'another_id', but I can't seem to figure out how to also have access to 'my_id' originally generated from the page1.php.

Is using the superglobals the way to go here, and if so, can I get a sample line of code to show how to keep 'my_id' available? Or is there another technique I am overlooking?

thanks much,

tc
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Passing a parameter/variable between pages

Post by Celauran »

You can insert the value of $_GET['my_id'] into the link

Code: Select all

<a href="www.mydomain.com/page3.php?my_id=<?php echo $_GET['my_id']; ?>&another_id=4">Link</a>
Or you could use session data or cookies.
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Passing a parameter/variable between pages

Post by wtc »

Thank you!! worked like a charm. Didn't realize you could pass more than one variable on the URL line. Am good to go!

tc
Post Reply