Passing variables from one page to another

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
WhiteWolf
Forum Newbie
Posts: 7
Joined: Tue Jun 17, 2008 4:10 pm

Passing variables from one page to another

Post by WhiteWolf »

I have a page, galleries.php. It consists of thumbnail pictures, each representing a gallery of photos. The user clicks on a thumbnail which is linked to another page, results.php which searches a database, and displays all matching results. Here is the dilemma:
In the galleries page, I have the following -

Code: Select all

 
 <td><div align="center"><a href="showGallery.php?<?PHP echo(session_id()) ?>"><img name="igallery1" src="images/Gallery/image.jpg" width="164" height="116" alt="Birds of Prey" border=0></div></a><br><CENTER> gallery 1</CENTER></td>
                 
 


which passes the session ID from Galleries to showGallery.php. My question is, since the thumbnail is the link to the next page (i.e. showGallery), how can I specify which gallery was clicked? I can't specify it as a session variable, because as is, the script leaves the galleries.php page before I run any sort of check. I know it must be obvious, but I'm drawing a blank. Can anyone tell me a solution to this problem?
User avatar
Jasheppard
Forum Newbie
Posts: 24
Joined: Tue Jun 17, 2008 11:44 pm

Re: Passing variables from one page to another

Post by Jasheppard »

Well first of all, you are using incorrect html

you have:
<td><div align="center"><a href="showGallery.php?<?PHP echo(session_id()) ?>"><img name="igallery1" src="images/Gallery/image.jpg" width="164" height="116" alt="Birds of Prey" border=0></div></a><br><CENTER> gallery 1</CENTER></td>
which needs to be:

Code: Select all

<td><div align="center"><a href="showGallery.php?<?PHP echo session_id(); ?>"><img name="igallery1" src="images/Gallery/image.jpg" width="164" height="116" alt="Birds of Prey" border=0></a></div><br><CENTER> gallery 1</CENTER></td>
(code=html stuffs up on this lot?)

what i have changed:
1. you have a 'a' tag before the after the div but your ending it after the div? </div></a>
New: </a></div>
2. you need a semicolon ';' after the php code to end that command.
Old: <?PHP echo(session_id()) ?>
New: <?PHP echo(session_id()); ?> or <?PHP echo session_id(); ?>
I have never seen echo() before but echo ""; works

i have only been doing php for about a month now but yea.. hope that works.
i like debugging code 8)
Last edited by Jasheppard on Thu Jun 26, 2008 2:32 am, edited 4 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Passing variables from one page to another

Post by Benjamin »

Well the session id is being passed, but it's currently not assigned to a variable.

You're looking for something more along the lines of:

Code: Select all

 
<td><div align="center"><a href="showGallery.php?sid=<?php echo(session_id()); ?>"><img name="igallery1" src="images/Gallery/image.jpg" width="164" height="116" alt="Birds of Prey" border=0></a></div><br><CENTER> gallery 1</CENTER></td>
I added sid=
User avatar
Jasheppard
Forum Newbie
Posts: 24
Joined: Tue Jun 17, 2008 11:44 pm

Re: Passing variables from one page to another

Post by Jasheppard »

good point, that would help
WhiteWolf
Forum Newbie
Posts: 7
Joined: Tue Jun 17, 2008 4:10 pm

Re: Passing variables from one page to another

Post by WhiteWolf »

Thanks for the input. here's the thing, even if I assign the session ID to a variable like sid, how can I tell the script which gallery to display? In my example, I only posted one gallery, but there's actually 7 of them. I know normally you can pass variables in teh SESSION array, but here, once the user clicks on a thumbnail, it exits the gallery.php file, and launches the showGallery.php file. I don't see a way to say the user clicked on gallery1, or gallery2. The session ID will be passed, but how can I also pass a variable containing the gallery name?
User avatar
Jasheppard
Forum Newbie
Posts: 24
Joined: Tue Jun 17, 2008 11:44 pm

Re: Passing variables from one page to another

Post by Jasheppard »

I don't fully understand what you mean, but does this help?

<a href="showGallery.php?sid=<?PHP echo session_id(); ?>&galleryname=igallery1">

:?: :?: :?:
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Passing variables from one page to another

Post by WebbieDave »

Jasheppard wrote:<a href="showGallery.php?sid=<?PHP echo session_id(); ?>&galleryname=igallery1">
Right. Then you're showGallery.php can:

Code: Select all

if ($_GET['galleryname'] == 'igallery1') {
    // do stuff with igallery1
}
or something like that.
Post Reply