Data loss in session

2

I'm doing a form with steps in Ajax. The idea is at every step to get the form data, play an array, and store the array in the session, so I can manipulate all the data in the last step:

Ajax form1:

$('.next').click(function(){
   $.post({
      url: '/teste_mvc/turma/step1/' ,
      data: $('#step1').serialize(),
      success: function (e){alert(e);}
});

Action step1

public function Step1(){
    session_start();
    $ses = array();
    foreach($_POST as $k=>$p){
        $ses[$k] = $p;
    }
    $_SESSION['step1'] = $ses;
    print_r($_SESSION['step1']);
    exit;
}

So far the application works as it should, problem is with the second step onwards:

Ajax form2:

 $('.next').click(function(){
     $.post({
         url: '/teste_mvc/turma/step2/' ,
         data: $('#step2').serialize(),
         success: function (e){alert(e);}
      });
 });

Action step2

 public function Step2(){
    session_start();
    $ses = array();
    foreach($_POST as $k=>$p){
        $ses[$k] = $p;
    }
    $_SESSION['step2'] = $ses;
    print_r($_SESSION['step1']);
    print_r($_SESSION['step2']);
    exit;
}

When returning session data back to ajax, the value of $_SESSION['step2'] PHP can retrieve, but the $_SESSION['step1'] data is lost, with PHP returning undefined to it.

I must be making a silly mistake, but I'm trying to make it work here and everything I know has already been applied.

    
asked by anonymous 18.05.2016 / 18:18

1 answer

0

The problem with the code is quite simple and it really sucks, you are restarting the sessions, session_start() should only be used once. You can very much call it at the beginning of the code. I strongly do not recommend using it within a function!

Solution: What you might be doing and by session_start() outside functions, starting only once. At the beginning of the code preferably!

    
30.05.2016 / 17:55