View Full Version : save entire form to session? [RESOLVED]

06-21-2005, 04:54 PM
Hi! I have a HUGE form (upwards of 100 entries - not ideal, but for certain other reasons it needs to be done this way). I need to store the submitted values in a session for later use. There has got to be a better way than the system I'm using now:

$_SESSION[name] = $_POST[name];
$_SESSION[phone] = $_POST[phone];etc...
This format makes for a very daunting update process, not to mention the carpal tunnel that will likely come from typing it all. Is there a way to just, across the board, store all the values from a form to a session variable of the same name automatically? This would really help!


SeeIT Solutions
06-21-2005, 04:57 PM
I'm just guessing but would this work

$_SESSION['form'] = $_POST

06-21-2005, 04:58 PM
Well you could probably just do $_SESSION = $_POST, although that will probably overwrite any existing sessions you have that are not related to the form so may not work for what you want.

You could try:

foreach($_POST as $key => $value) {
$_SESSION[$key] = $value;

06-21-2005, 05:21 PM
Thanks, everyone! delinear, worked great. Now, with that code, is it possible to later print all those session varialbes without going through the same manual labor as I mentioned above?
foreach($_POST as $key => $value) {
$_SESSION[$key] = $value;
echo $key.' = '.$_SESSION[$key].'<br>';
} works immediately after the form is submitted, but I'm thinking some time after the form has been submitted. You couldn't do a foreach() with the session, could you?

06-21-2005, 05:25 PM
Sure, create it as a multidimensional array:

foreach($_POST as $key => $value) {
$_SESSION['form_name'][$key] = $value;

Now, you have a session array named form_name, or whatever you want. Create a foreach on the $_SESSION['form_name'] to retrieve individual values or use extract() function.

Oh, and in response to your question, you can use foreach on the $_SESSION superglobals, it just wouldn't have any control and will include all $_SESSION variables within it.

Just though about this as well!
You can always serialize your $_POST array and save it as a single key within the $_SESSION superglobal. Later unpack it using unserialize.

06-21-2005, 11:20 PM
Thanks! works great - issue solved. You did it again!