passing variables to a different page

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
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

passing variables to a different page

Post by nhan »

hope anyone can help me, im new to php..
i have 3 pages of php first is the index.php that contains the code :
header("Location:main.php?userName=$_POST[userName]");
i passed the variable userName to the next page main.php and was able to diplay the variable userName.
print "<html><body>Welcome $_GET [userName],<br>\n</body></html>";

my problem is how can i pass the same variable to a next page, i will be using forms on this one, if they click the button the same variable would be submitted to the 3rd php page...

thanks so much in advance....

regards,

nhan
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

I suggest using sessions its better then passing it through the url
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

but u can use the the get method to pass them with the url
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

i have tried using session but i have this error :
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /usr/local/apache/login/main.php:6) in /usr/local/apache/login/main.php on line 37

what seems to be the problem....

in my main.php line 37 >>> session_start();

thanks...
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

line 1of your script should be <? ob_start(); ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ob_start() is a band-aid approach to fixing issues with "already sent header" problems. Have a read in our tutorial: viewtopic.php?t=1157
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

i have tried the ob_start();, but i have to print the session name...
i defined the session as :

$_SESSION['user'] = $_POST['userName'];

on the second page :

ob_start();
print $_SESSION["user"];
but it does not output the value of the userName

on the third page :
i have to display again the the variable userName...

thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

session_start() must be on all pages you wish to use session data on.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

If the next page has a form that posts to the server, you can store the variables in hidden text fields. That way they will be part of the $_POST array when it goes to the next page and you don't have to futz with sessions.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

$user = $_GET['userName'];
...
....
.....
<input type='hidden' name='userName' value='$user'>

is this correct??

thanks! im very sorry for these questions, i just want to learn...
thanks so much!

--nhan
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Yes, that looks good. Don't worry, asking questions is how you learn.

What I do out of laziness is write a function like this - I typed this on the fly, so if there are typos excuse me. You can do the same for $_POST obviously.

Code: Select all

function getVar($GetVariableName, $Default = '') &#123;
  if (isset($_GET&#1111;$GetVariableName])) &#123;
    $result = $_GET&#1111;$GetVariableName];
  &#125; else &#123;
    $result = $Default;
  &#125;
  return $result;
&#125;
Or the shorthand.

Code: Select all

function getVar($GetVariableName, $Default = '') &#123;
  return (isset($_GET&#1111;$GetVariableName])) ? $_GET&#1111;$GetVariableName] : $Default;
&#125;

The idea here is if a $_GET variable is missing, you won't get any error or warning, you will get a default value.

So you could call it like this:

Code: Select all

$user = getVar('userName');

$OK = getVar('isOK', false);
Last edited by smpdawg on Sun Feb 27, 2005 10:29 pm, edited 1 time in total.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

thanks so much for the code, i was able to display the value using the $GET method on the second page... but one thing, i was not able display the said value on the third php page...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to continue passing the data along the pages, in some fashion.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Was it part of a form that was processed by clicking the submit button? If so, it is likely in the $_POST array. You can find it out quickly by doing this on that page that didn't work.

Code: Select all

print_r($_POST);
Then walk through your process and see if the value shows up on the page.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

Thanks so much guys! I owe one on all of you... passing of variables is now ok, thanks smpdawg for the codes. i used ur codes, then submitted the values using hidden fields. thanks so much! i hope u never get tired of answering questions like this one....

:D :D :D

thanks again...

nhan
Post Reply