2

I have a survey script that has 3 questions per page. When users answer the questions on the first page and click next, the data from the previous page is stored in $_SESSION['survey']['data'] by doing this:

$data = postToArray($_POST, $ignore_fields);
$_SESSION['survey']['data'] = $data;

$data is an array that looks like:

array('question' => 'answer', 'question' => 'answer');

postToArray does a few checks and manipulates the actual submission a bit, before returning it to $data.

When the user is on page two of the survey, the same thing happens over. I assumed that when $data gets added to the session, via $_SESSION['survey']['data'] = $data;, that it would append to the session array if the 'question' (key) did not exist, but if it did (because the user went to a previous page and changed their answer), that the existing value with the same key would be overwritten, however the last page's submission overwrites everything in the ['data'] array in the session. Come to think about it, that makes perfectly sense.

I tried various things, such as retrieving the $_SESSION['survey']['data'], storing it in array, reading the last submission, merging the arrays, and then re-saving everything in the SESSION, but my code didn't work out -- does this approach make sense? Is that possible?

I also tried array_push, but no luck there.

In addition, I tried adding to $_SESSION['survey']['data'][], which at least saves everything (each submission in its own array), but then if the user goes back a page, any values they change and re-submit are added as another array.

Preferably, I'd like one giant array with all questions/answers and it keeps adding to that array and overwrites any values with existing keys.

What's the best approach?

Thanks, -Ryan

SOLUTION IMPLEMENTED

$data = postToArray($_POST, $ignore_fields);
foreach($data as $question => $answer)
{
    $_SESSION['survey']['data'][$question] = $answer;
}
2
  • 2
    it rewrites the $_SESSION['survey']['data'] because you are assigning it a new value . use $_SESSION['survey']['data'][$data['question']] = $data['answer'] Commented Jan 19, 2011 at 17:29
  • That worked beautifully! I can't believe I missed that :) Commented Jan 19, 2011 at 17:44

3 Answers 3

5

Try to serialize the data before saving it in a session variable.

http://php.net/manual/en/function.serialize.php

Sign up to request clarification or add additional context in comments.

1 Comment

I looked at the doc, but don't quite understand what it does to help?
0

Loop through the $data array and set it up as naiquevin stated, $_SESSION['survey']['data'][$data['question']] = $data['answer'].

Comments

0
 <?php
  session_start();
  if(!isset($_POST["submit"])){
  $_SESSION["abc"]=array("C", "C++","JAVA","C#","PHP");
  }

   if(isset($_POST["submit"])){
   $aa=$_POST['text1'];

   array_push( $_SESSION["abc"],$aa);
   echo "hello";
    foreach( $_SESSION["abc"] as $key=>$val)
    { 

        echo $val;
    }
  }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.