0

My question is that when I copy my array elements between different php scripts using session variables, nothing gets printed out. The following are my two php files.

file1.php

   $SQL = "SELECT * FROM tblquestions";
if ($db_found) {
        $result = mysql_query($SQL);
        $numRows = mysql_num_rows($result); //return number of rows in the table
        echo '<FORM NAME ="form1" METHOD ="POST" ACTION ="file2.php">';
        for ($i = 1; $i <= 2; $i++)
        {
            $db_field = mysql_fetch_assoc($result);
            $qID[$i] = $db_field['QID'];
            $question[$i] = $db_field['Question'];
            $A[$i] = $db_field['qA'];
            $B[$i] = $db_field['qB'];
            $C[$i] = $db_field['qC'];
            echo '<P>';
            print $question[$i];
            echo '<P>';
            echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= 'A'>"; 
            print $A[$i];
            echo '<P>';
            echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'B'>"; 
            print $B[$i];
            echo '<P>';
            echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'C'>"; 
            print $C[$i];
            //if (isset($_POST[$name_Value]))
            $survey_Answers[$i-1] = $_POST[$qNum];
            print '</BR>'.$survey_Answers[$i-1]."</BR>";
            $question_Number = ltrim($qNum,'q');
            $question_Number++;
            $qNum ='q'.$question_Number;
        }

        echo '<p>';
    session_start();
    $_SESSION['answers'] = $survey_Answers;
        echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';

        echo '</form>';

On my Second file (file2.php), I have the following:

<?PHP
session_start();
if (isset($_POST['Submit1'])) {
        $results = $_SESSION['answers'];
        print $results[0];
?>

Nothing gets printed out when executing the above code.

Thanks in advance

13
  • 3
    Why aren't you simply saving that to a session variable? Anything in HTML document can be manually modified. Commented Jun 24, 2013 at 14:23
  • Not sure if it's a typo in the question, but you're missing an = after value Commented Jun 24, 2013 at 14:24
  • When for the first time you submit the form, //if (isset($_POST[$name_Value])) $survey_Answers[$i-1] = $_POST[$qNum]; print '</BR>'.$survey_Answers[$i-1]."</BR>"; $question_Number = ltrim($qNum,'q'); $question_Number++; $qNum ='q'.$question_Number; this part works for the first time. so the next time you submit the form, you'll get value for $Survey_answers not on the first time. You can simply work with the radio input, instead of relying another array. Commented Jun 24, 2013 at 14:52
  • do you mean adding a hidden form element in my first for loop. Again, don't I need an array to hold these multiple values as I'm going to use them on another php script. Commented Jun 24, 2013 at 14:56
  • @N.B. Maybe that's what he wants to do Commented Jun 24, 2013 at 14:59

1 Answer 1

1

I am pretty sure that the session values stored need to be strings.

In file1.php change:

$_SESSION['answers'] = $survey_Answers;

to this:

$_SESSION['answers'] = json_encode($survey_Answers);

Then in file2.php change:

$results = $_SESSION['answers'];

to this:

$results = json_decode($_SESSION['answers']);
Sign up to request clarification or add additional context in comments.

14 Comments

I tried this. However, I modified my action to file1.php. So I typed the following command: echo $_SESSION['answers'][0]; and the output was a square bracket.
In file1 just after: $_SESSION['answers'] = json_encode($survey_Answers); add this line and post the results: echo var_dump($_SESSION['answers']);
actually, what is making me crazy is that when I use any other variable that is not Array type. Everything works correctly.
this is the output of executing the following: echo var_dump($_SESSION['answers']); string '[]' (length=2)
It looks like the $survey_Answers variable is an empty array. To confirm, can you do a var_dump($survey_Answers); just above the code $_SESSION['answers'] = json_encode($survey_Answers);
|

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.