PHP Sessions and Forms

Discussion in 'PHP' started by srandal1, Jul 14, 2008.

  1. #1
    Hello,

    Complete NEWB here, and have been struggling with this one for a while. Any help is much appreciated.

    I have a multipage form. The Submit is sent to an intermediate page which basically looks like this:

    if( !isset( $_SESSION ) ) { session_start(); }
    ini_set('session.cache_limiter', 'private');
    foreach($_POST as $key=>$value) {
    $_SESSION['system'][$key]=$value;
    }
    header('Location: http://www.domain.com/form2.php');

    The variable is in the Session cookie and if I were to

    echo $_SESSION['system']['name']

    it will output the name submitted on the previous form. What I am trying to accomplish is if a user goes back to page 3 of the page 5 form, the input fields will be pre-populated with the data the user first entered. My input field looks like this:

    <input type="text" name="name" value="<? echo $_SESSION['service']['name']; ?>" />

    However, no matter what I have tried I have been unable to make this work. I have this at the top of every page (just in case this is wrong).

    if( !isset( $_SESSION ) ) { session_start(); }
    ini_set('session.cache_limiter', 'private');

    I have also tried this and it did not work:

    <input type="text" name="name" value="<?= $_SESSION['service']['name']; ?>">

    If I use the back buttons the fields are populated, but I suspect that is a function of the browser and not the PHP.

    Thanks much for any suggestions.
     
    srandal1, Jul 14, 2008 IP
  2. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Try to put session_start(); on the top on every page. Forghet the if(!isset... ) thing.

    From what I see. If $_SESSION is set, session_start() will not happen, so the actual $_SESSION's will not show up.
     
    PET, Jul 14, 2008 IP
  3. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am not really sure whether we can put another dimensional array in $_SESSION.

    Usually what I did is to assign an array to a session for example like this:

    $my_array = array();
    $_SESSION['whatevername'] = $my_array;
    PHP:
     
    revvi, Jul 14, 2008 IP
  4. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #4
    It is possibile to have $_SESSION['something']['som']
     
    PET, Jul 14, 2008 IP
  5. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's cool to hear. I haven't tried that approach before. I always try to reduce session usage whenever possible.
     
    revvi, Jul 14, 2008 IP
  6. srandal1

    srandal1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks everyone for the input. I did try it with just

    session_start();
    PHP:
    and I get the same results. After a page is submitted, the session results are updated and you are sent to the next page through the redirect. If the name value was John and I go back and change it to Larry, and then do a
    echo $_SESSION['system']['name']
    PHP:
    it outputs Larry. But if I go back to that page, it will not populate the input box with 'Larry" - it is simply blank.

    Anything else I am missing or am I just going about this the wrong way?

    Thanks again
     
    srandal1, Jul 14, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I've seen loops like this so often, such a waste of resources, :eek: just use...

    foreach($_POST as $key=>$value) {
    $_SESSION['system'][$key]=$value;
    }
    PHP:
    $_SESSION['system'] = $_POST; // Much nicer 
    PHP:
    Done :D
     
    Danltn, Jul 15, 2008 IP
  8. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Yea. but if you use the code you told us, then you have a "hardcoded" code. What if you add another POST to the form, or you change the name of some of the input? You will have to modify the code again with the new name. This way you already have them in an array which you can manipulate.
     
    PET, Jul 15, 2008 IP
  9. srandal1

    srandal1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for all the input, but I still get nothing. Any other suggestions or methods? Can I get want I want by incorporating MYSQL?

    The main reason I want this is because people may need to go back and change answers. I really don't care about that but on the last page of the form they have to enter in contact information and I don't want them to have to retype that every time they make changes. I know it would aggrevate me if I had to do it.

    Thanks for any suggestions.
     
    srandal1, Jul 15, 2008 IP
  10. srandal1

    srandal1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ended up using client cookies and works great. Only way I could make it work so I will have to live with it.
     
    srandal1, Jul 16, 2008 IP