Page 1 of 1
passing variables to a different page
Posted: Sun Feb 27, 2005 8:33 pm
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
Posted: Sun Feb 27, 2005 8:53 pm
by PrObLeM
I suggest using sessions its better then passing it through the url
Posted: Sun Feb 27, 2005 8:59 pm
by shiznatix
but u can use the the get method to pass them with the url
Posted: Sun Feb 27, 2005 9:10 pm
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...
Posted: Sun Feb 27, 2005 9:12 pm
by shiznatix
line 1of your script should be <? ob_start(); ?>
Posted: Sun Feb 27, 2005 9:17 pm
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
Posted: Sun Feb 27, 2005 9:19 pm
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!
Posted: Sun Feb 27, 2005 9:22 pm
by feyd
session_start() must be on all pages you wish to use session data on.
Posted: Sun Feb 27, 2005 9:23 pm
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.
Posted: Sun Feb 27, 2005 9:37 pm
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
Posted: Sun Feb 27, 2005 9:43 pm
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 = '') {
if (isset($_GETї$GetVariableName])) {
$result = $_GETї$GetVariableName];
} else {
$result = $Default;
}
return $result;
}
Or the shorthand.
Code: Select all
function getVar($GetVariableName, $Default = '') {
return (isset($_GETї$GetVariableName])) ? $_GETї$GetVariableName] : $Default;
}
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);
Posted: Sun Feb 27, 2005 9:57 pm
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...
Posted: Sun Feb 27, 2005 10:03 pm
by feyd
you have to continue passing the data along the pages, in some fashion.
Posted: Sun Feb 27, 2005 10:04 pm
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.
Then walk through your process and see if the value shows up on the page.
Posted: Sun Feb 27, 2005 10:19 pm
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....
thanks again...
nhan