Click to See Complete Forum and Search --> : Passing variable values between PHP pages.


Mr Initial Man
07-02-2006, 04:43 AM
If I have a variable that I want to pass from one page to the next, what is the best way to do it?

The current method I am using is by URIs.

Page 1:
$chapter = $_GET['chapter'];
#...
echo('
#...
<li><a href="./glossary.php?fromchapter='.$chapter.'">Smilodon/English Dictionary</a></li>
#...
');


Page 2:

$from=$_GET['fromchapter'];
if(empty($from))
{
echo ('<li><a href="./fighter.php?chapter=0">Prologue</a></li>');
}
else
{
if($from==0)
{
echo ('<li><a href=./fighter.php?chapter='.$from.'>Back to Prologue</a></li>');
}
else
{
echo ('<li><a href=./fighter.php?chapter='.$from.'>Back to Chapter '.$from.'</a></li>');
}
}


But it seems to me there's got to be a better way to do this. Any suggestions?

GaryS
07-02-2006, 05:54 AM
Could use sessions?

bokeh
07-02-2006, 06:31 AM
I really believe the method you are using is the best. The purpose of the query string is retrieval and that is exactly what you are using it for.

Mr Initial Man
07-02-2006, 12:49 PM
If $_GET['fromchapter'] = 0, will the PHP read that the same as it being empty?

NogDog
07-02-2006, 01:19 PM
Yes: zero, an empty string, or the variable not being set will all return TRUE from the empty() function.

bokeh
07-02-2006, 05:04 PM
Not if you do this

if($_GET['fromchapter']==='0')

bokeh
07-03-2006, 02:06 AM
If zero is a valid user input, instead of checking the variable like this:if(!empty($_GET['fromchapter'])) check it like this:if(isset($_GET['fromchapter']) and is_numeric($_GET['fromchapter']))