By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

carrying over variables from a form to multiple pages...

P: n/a
I have some data from text fields that are being passed over through a
form that I am displaying with the $_POST superglobal. Once i have
echo'd out this data onto the next page, i'd like to continue to use
it on the next page. I haven't figured out how to do this properly
just yet, but I'm guessing it has something to do with sessions. I
have MANY variables being passed (over 100) and I'm hoping i don't
have to register each one of these manually with $_SESSION[].

example.

//first page (index.html)

<form action="form.php" method="post">
<select name="color"><option value="blue">blue</option><option
value="red">red</option>
<input name="submit" type="submit">

//new page (form.php)
<?php
echo "Bill's favorite color is {$_POST['color']}";
?>
////

Now say I want to use this $_POST['color'] value on another page that
the form.php takes you to next.....how do i continue to use this value
across multiple pages?

I'm assuming this is a trivial question, but i can't seem to figure it
out! Any help is much appreciated..thanks guys!

Jul 5 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Kurrent wrote:
I have some data from text fields that are being passed over through a
form that I am displaying with the $_POST superglobal. Once i have
echo'd out this data onto the next page, i'd like to continue to use
it on the next page. I haven't figured out how to do this properly
just yet, but I'm guessing it has something to do with sessions. I
have MANY variables being passed (over 100) and I'm hoping i don't
have to register each one of these manually with $_SESSION[].

example.

//first page (index.html)

<form action="form.php" method="post">
<select name="color"><option value="blue">blue</option><option
value="red">red</option>
<input name="submit" type="submit">

//new page (form.php)
<?php
echo "Bill's favorite color is {$_POST['color']}";
?>
////

Now say I want to use this $_POST['color'] value on another page that
the form.php takes you to next.....how do i continue to use this value
across multiple pages?

I'm assuming this is a trivial question, but i can't seem to figure it
out! Any help is much appreciated..thanks guys!
Two ways - make them hidden fields on each page (only works if you're
using forms), or store each one in the $_SESSION superglobal, i.e.
<?php
session_start();
$_SESSION['color'] = $_POST['color'];
?>

And when you want to use it:

<?php
session_start();
$color = $_SESSION['color'];
?>

Of course you will want to validate the entries to ensure they exist,
contain valid values, etc. (never assume $_POST data is accurate!).

But hopefully you get the idea.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 5 '07 #2

P: n/a
On Jul 4, 9:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Kurrent wrote:
I have some data from text fields that are being passed over through a
form that I am displaying with the $_POST superglobal. Once i have
echo'd out this data onto the next page, i'd like to continue to use
it on the next page. I haven't figured out how to do this properly
just yet, but I'm guessing it has something to do with sessions. I
have MANY variables being passed (over 100) and I'm hoping i don't
have to register each one of these manually with $_SESSION[].
example.
//first page (index.html)
<form action="form.php" method="post">
<select name="color"><option value="blue">blue</option><option
value="red">red</option>
<input name="submit" type="submit">
//new page (form.php)
<?php
echo "Bill's favorite color is {$_POST['color']}";
?>
////
Now say I want to use this $_POST['color'] value on another page that
the form.php takes you to next.....how do i continue to use this value
across multiple pages?
I'm assuming this is a trivial question, but i can't seem to figure it
out! Any help is much appreciated..thanks guys!

Two ways - make them hidden fields on each page (only works if you're
using forms), or store each one in the $_SESSION superglobal, i.e.

<?php
session_start();
$_SESSION['color'] = $_POST['color'];
?>

And when you want to use it:

<?php
session_start();
$color = $_SESSION['color'];
?>

Of course you will want to validate the entries to ensure they exist,
contain valid values, etc. (never assume $_POST data is accurate!).

But hopefully you get the idea.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
ack, this is close to what i originally did...on my second page i made
all the default values in a form the carried over values, but i have
so many variables i was hoping i don't have to register them one-by-
one. Looks like i'm gonna have to rename my values and make some sort
of loop to register them all.

Jul 5 '07 #3

P: n/a
Message-ID: <11*********************@d30g2000prg.googlegroups. comfrom
Kurrent contained the following:
>ack, this is close to what i originally did...on my second page i made
all the default values in a form the carried over values, but i have
so many variables i was hoping i don't have to register them one-by-
one. Looks like i'm gonna have to rename my values and make some sort
of loop to register them all.
Why rename?

foreach($_POST as $key=>$value){
//insert sanity check here
$_SESSION[$key]=$value;
}
But I'd probably do
foreach($_POST as $key=>$value){
//insert sanity check here
$_SESSION['data'][$key]=$value;
}

It's then a lot easier to get rid of the POSTed variables if you need to
ie unset($_SESSION['data'])

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 5 '07 #4

P: n/a
Kurrent wrote:
On Jul 4, 9:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Kurrent wrote:
>>I have some data from text fields that are being passed over through a
form that I am displaying with the $_POST superglobal. Once i have
echo'd out this data onto the next page, i'd like to continue to use
it on the next page. I haven't figured out how to do this properly
just yet, but I'm guessing it has something to do with sessions. I
have MANY variables being passed (over 100) and I'm hoping i don't
have to register each one of these manually with $_SESSION[].
example.
//first page (index.html)
<form action="form.php" method="post">
<select name="color"><option value="blue">blue</option><option
value="red">red</option>
<input name="submit" type="submit">
//new page (form.php)
<?php
echo "Bill's favorite color is {$_POST['color']}";
?>
////
Now say I want to use this $_POST['color'] value on another page that
the form.php takes you to next.....how do i continue to use this value
across multiple pages?
I'm assuming this is a trivial question, but i can't seem to figure it
out! Any help is much appreciated..thanks guys!
Two ways - make them hidden fields on each page (only works if you're
using forms), or store each one in the $_SESSION superglobal, i.e.

<?php
session_start();
$_SESSION['color'] = $_POST['color'];
?>

And when you want to use it:

<?php
session_start();
$color = $_SESSION['color'];
?>

Of course you will want to validate the entries to ensure they exist,
contain valid values, etc. (never assume $_POST data is accurate!).

But hopefully you get the idea.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

ack, this is close to what i originally did...on my second page i made
all the default values in a form the carried over values, but i have
so many variables i was hoping i don't have to register them one-by-
one. Looks like i'm gonna have to rename my values and make some sort
of loop to register them all.
Geoff has some good ideas on how to handle it. But you don't have to
register variables in the session. Just set the key/value pair in the
$_SESSION variable, as above.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.