1

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

<?PHP
     session_start();
        $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['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];
}
?>

However, on my file2.php I get the following error: Undefined offset: 0 and nothing gets printed out.

3
  • If conditional is not closed in the second file Commented Jun 24, 2013 at 18:12
  • Are you getting inside that if block ? Commented Jun 24, 2013 at 18:12
  • missing the curly bracket is a type that I don't have in my original code. Commented Jun 24, 2013 at 18:20

5 Answers 5

1
echo '<p>';
session_start();

that can´t work, session_start has to be called before any output! if you put session_start() at the beginning of your file, you should be all right.

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

1 Comment

you did put it before ANY output in both files? there mustn´t be any echo/print/var_dump or pure html or an empty line before the php opening tag before the call
1

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

source: http://php.net/manual/en/function.session-start.php

You need to call session_start() before you output anything. It is a best practice to place it at the beginning of your script before you output anything.

5 Comments

I moved session_start() in file1.php to the top but it isn't working either.
I'm getting the following error instead: Undefined offset: 0 in file2.php
I'm not sure what to tell you. My guess is that the first file is not being executed before you access the second file. It could be something else too. There are a lot of things that could go wrong in the code that you provided.
I'd actually guess the same thing. However, when I change the action on my first script to the same script and echo the $_SESSION variables, it does work correctly.
Also, when I change my variable to a non array type, things works fine. Any other clues?
0

Please Add the below code to every page you want to use the session data.Other wise it will return an error .

<?php
session_start();
?>

4 Comments

Check the file is located on the same domain .
Dear All, I've modified my code to reflect the changes you mentioned earlier. Any other thoughts?
"Check that the file is on the same domain"? Really? If that's the level of advice this is all about, then ("Please") also try: Have you tried turning it off and on again? Are you sure you haven't accidentally deleted the files? Make sure PHP is installed on your server. Make sure PHP has read/write permissions to the filesystem. Make sure you're not in a coma having a coding nightmare. If that still doesn't fix it, hire an expensive professional. @AhmedEbaid you have my condolences, I have a similar problem and no fix. Given the year and lack of solution, have you found any real answer yet?
@kittenCodings, unfortunately, my memory won't serve 4 years back in time. However, looking at the history it doesn't sound like there was an answer.
0

in file1.php session_start(); should be first line of the code same as in file2.php

4 Comments

that is what I currently have.
If not working then check value of $db_found, I think if ($db_found) { code block is not running hence $survey_Answers is also not getting set.
I don't have a problem connecting to the database. That condition works fine. Actually, if instead I changed the action to file1.php, I can read this session variable on page1 but not on page2.
Make sure there is no white space before opening php tag <?PHP in page2
0

There is a comment on the PHP manual page for session_start() which sounds very similar to your problem. http://php.net/manual/en/function.session-start.php#65944

It says that an array with integer keys assigned either as a straight array or using the integers will fail and the data will not pass to the next page.

Modify your code to use strings as keys.

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.