Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    save entire form to session? [RESOLVED]

    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:
    PHP Code:
    $_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!

    Thanks!!!
    Last edited by mtd; 06-21-2005 at 11:21 PM.

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I'm just guessing but would this work

    PHP Code:
    $_SESSION['form'] = $_POST 

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    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:
    PHP Code:
    foreach($_POST as $key => $value) {
        
    $_SESSION[$key] = $value


  • #4
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    PHP Code:
    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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sure, create it as a multidimensional array:
    PHP Code:
     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.

    Edit:
    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.
    Last edited by Fou-Lu; 06-21-2005 at 05:27 PM.

  • #6
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! works great - issue solved. You did it again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •