pass a variable 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

tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

pass a variable from one page to another

Post by tarja311 »

Hi all.

I'm trying to pass a variable from one page to another but having a difficult time doing so. I have two pages. Page 1 pulls data in from a database and displays it. Page 2 is simply the "more detailed info" type page that gets displayed when the mouse is hovered over the results ( in a tooltip kind of fashion ). Now, the mouse-hovering and detailed info works great. The only problem i am having is not being able to pass the $id from Page1 to Page2 where it can show this info automatically cause right now, in Page2, i have to type in the $id manually ( eg : $id = 32 ) to get a result.

I am not sure what to do now.

Page 1 - pulling data in from my database

Code: Select all

while($r = mysql_fetch_array($sql))
{
	$id		=	$r["F_ID"];
	$file		=	$r["F_FILENAME"];

	echo "<TD><A HREF = '?w=result&cmd=filename&id=$id' onmouseover = ajax_showTooltip('tooltip.php',this);return false onmouseout=ajax_hideTooltip()>". $file ."</A></TD>";	
}


If more info is needed i'll be more than happy to give it.

Thanks.

-- tarja
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

use a session?
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

If you mean session_start(); , that is already in place at the top of both pages.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Does the right id= show up in the url when you press the link?
tarja311 wrote:in Page2, i have to type in the $id manually ( eg : $id = 32 ) to get a result.
What's your "not-manually" code? Probably contains something like $id=$_GET['id']. Check the parameters via var_export($_GET);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

anything you store inside the $_SESSION array will be preserved for the whole session
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Does the right id= show up in the url when you press the link?
Yes. i receive a : ?w=result&cmd=filename&id=$id=31
What's your "not-manually" code? Probably contains something like $id=$_GET['id']. Check the parameters via var_export($_GET);
In Page 2 i am just calling to the database and declaring the variable myself like so :

Code: Select all

$id = 31;
$result  = mysql_query("SELECT * FROM `". $my_result_table. "` WHERE `F_ID` = '$id'");
I do not have any $_GET's because i thought i needed a form to use them. :-S
jammr
Forum Newbie
Posts: 16
Joined: Mon Jan 22, 2007 12:10 am

Post by jammr »

Just store the ID variable on the first page in a session and in the second page use the session variable.
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Ok. I put it in a session as follows ( stop me if i'm using this incorrectly ) :

Page 1

Code: Select all

while($r = mysql_fetch_array($sql))
{
        $id          =    $r["F_ID"];
        $file      =  $r["F_FILENAME"];

        echo "<TD><A HREF = '?w=result&cmd=filename&id=$id' onmouseover = ajax_showTooltip('tooltip.php',this);return false onmouseout=ajax_hideTooltip()>". $file ."</A></TD>";       

$_SESSION['id'] = $id_file;

}
Page 2

Code: Select all

$id_file = $_SESSION["id"];

$result  = mysql_query("SELECT * FROM `". $db_xoc_table. "` WHERE `F_FILE_ID` = '$id_file'");
So far it is only picking up the first result. It does not seem to be dynamic at the moment. Any ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you want
a) to pass an id with each url so details are displayed according to the link that was activated/clicked?
b) to store one id in the session data so that no matter what link was clicked the details of this one record is displayed?
c) both?
Last edited by volka on Mon Jan 22, 2007 1:27 pm, edited 2 times in total.
jammr
Forum Newbie
Posts: 16
Joined: Mon Jan 22, 2007 12:10 am

Post by jammr »

Code: Select all

$id_file = $_SESSION["id"];

$result  = mysql_query("SELECT * FROM `". $db_xoc_table. "` WHERE `F_FILE_ID` = '$id_file'");

Check the session variable in this bit again... should be $_SESSION['id'] :D
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

I appreciate all the help so far.

Using $_SESSION still only displays details of the first record inside the tooltip, even with it inside the while-loop. I guess it does not refresh itself ( static ) when i hover my mouse over a different result.
Do you want
a) to pass an id with each url so details are displayed according to the link that was activated/clicked?
I guess that is what i am trying to do. However, the link does not get clicked, it gets hovered over, displaying the detailed tooltip. As i mentioned, so far i am able to retreive the detailed info, but only if i specify the id myself. :?
Mohamed
Forum Newbie
Posts: 21
Joined: Fri Jan 19, 2007 6:59 pm
Location: Seattle

Post by Mohamed »

tarja311 wrote:
Do you want
a) to pass an id with each url so details are displayed according to the link that was activated/clicked?
I guess that is what i am trying to do. However, the link does not get clicked, it gets hovered over, displaying the detailed tooltip. As i mentioned, so far i am able to retreive the detailed info, but only if i specify the id myself. :?
then pass variable like this way http://example.com/tooltip.php?id=whatever_id.

and use $_GET['id'] to retrieve the value of the variable.

like this way

Code: Select all

if(isset($_GET['id']){
$id = $_GET['id'];
}
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

then pass variable like this way http://example.com/tooltip.php?id=whatever_id.

and use $_GET['id'] to retrieve the value of the variable.
Stupid question, but how do i make my url like that if i am not clicking on the results, but hovering over them with my mouse? Or maybe your suggestion is unclear to me.

Anyways i did try as u suggested but to no avail. :?

Page 1's url is like so : http://site/?w=result while Page 2's url is not attached in this way. it is accessed as just 'tooltip.php'.
Mohamed
Forum Newbie
Posts: 21
Joined: Fri Jan 19, 2007 6:59 pm
Location: Seattle

Post by Mohamed »

your code

Code: Select all

while($r = mysql_fetch_array($sql))
{
        $id          =    $r["F_ID"];
        $file      =  $r["F_FILENAME"];

        echo "<TD><A HREF = '?w=result&cmd=filename&id=$id' onmouseover = ajax_showTooltip('tooltip.php',this);return false onmouseout=ajax_hideTooltip()>". $file ."</A></TD>";       
}
modified one

Code: Select all

while($r = mysql_fetch_array($sql))
{
        $id          =    $r["F_ID"];
        $file      =  $r["F_FILENAME"];

        echo "<TD><A HREF = '?w=result&cmd=filename&id=$id' onmouseover = ajax_showTooltip('tooltip.php?id=$id',this);return false onmouseout=ajax_hideTooltip()>". $file ."</A></TD>";       
}
tooltip.php?id=$id

so in tooltip.php

use this code to get value of id

Code: Select all

if(isset($_GET['id'])){
    $id = $_GET['id'];
}
is that clear?
Mohamed
Forum Newbie
Posts: 21
Joined: Fri Jan 19, 2007 6:59 pm
Location: Seattle

Post by Mohamed »

tarja311 wrote: Stupid question, but how do i make my url like that if i am not clicking on the results, but hovering over them with my mouse? Or maybe your suggestion is unclear to me.

Anyways i did try as u suggested but to no avail. :?

Page 1's url is like so : http://site/?w=result while Page 2's url is not attached in this way. it is accessed as just 'tooltip.php'.
if you can load tooltip.php, why not tooltip.php?id=20
Post Reply