1

Goal: I want to pass an array with values from a page to another.

Problem: When it comes to the second page and i tried to print_r the array it doesn't have any values. and it gives me the this php notice on the second page:

Notice: Undefined index: answers in Questionnaire_Category_2.php on line 17

here is my snippet code from the first page which is Questionnaire_Category_1.php

if(isset($_POST['Choice'])){ //When the user submits the answer
    $Answer[] = $_POST['Choice']; //All answers from the form will be placed in the array
    session_start();
    $_SESSION['answers'] = $Answer; //All values of the array will be assigned to the session variable 'answers'
    header("Location: Questionnaire_Category_2.php");
}

This is the snippet code of the second page named Questionnaire_Category_2.php

$ANSWER[] = $_SESSION['answers']; //Get all the values from the session and store it in array and also this is the Line 17 that causes a notice message
echo "<pre>"; print_r($ANSWER); echo "</pre>";

Questions:

  1. Is my implementation from both pages are right?
  2. If not, how do i properly pass an array along with its values from one page to another?
  3. Is there another way to do it besides using $_SESSION?

1 Answers1

1

do session_start() before adding and getting values in session

session_start();
$_SESSION['answers'] = $ANSWER;

and

session_start();
$ANSWER = $_SESSION['answers'];
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71