Log in

View Full Version : pass variables between pages



sujith787
09-12-2007, 01:02 AM
i have a page first.php in this i have 7 veriables like $1, $2, $3, $4, $5, $6, $7. i have another page second.php i want to pass some variables from first.php to second.php.

with out using the form submission.

is there anyway.

thetestingsite
09-12-2007, 01:19 AM
You'r best bet would be to use either sessions, cookies, or by using the GET method (second.php?1=$1&2=$2...). If you were to use sessions, you could do something like the following:

first.php


<?php
session_start();

$_SESSION['1'] = 'variable 1';
$_SESSION['2'] = 'variable 2';

//rest of code
?>




<?php
session_start();

echo $_SESSION['1'];
echo '<br><br>';
echo $_SESSION['2'];

//rest of code
?>


Hope this helps.

sujith787
09-12-2007, 01:29 AM
thanks.

i have link in first.php

<a href="second.php?id=<?php echo $1; ?>">CLICK HERE </a>

by this i can get the $1 variable in second.php

like this.

<a href="second.php?id=<?php echo $1; ?>&amp;two=<?php echo $2; ?>&amp ">CLICK HERE </a>

am i rite....

plz reply me the exact code for that GET method.

thetestingsite
09-12-2007, 01:31 AM
That is how you would send the variables to the second php page, yes. Then in the second php page; in order to extract those variables, you would want to do something like this:



<?php
$1 = $_GET['id'];
$2 = $_GET['two'];

//and so on
?>


Hope this helps.

sujith787
09-12-2007, 09:21 PM
thank you

djr33
09-13-2007, 08:47 AM
$1 is invalid, by the way. Must start with a letter. (Or, system variables use the reserved _ start, like $_GET.)