通过html隐藏表单元素将PHP中的数组传递给另一个PHP脚本

时间:2013-06-24 14:21:48

标签: php html

我的问题是,当我使用会话变量在不同的PHP脚本之间复制我的数组元素时,没有任何内容被打印出来。以下是我的两个php文件。

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>';

在我的第二个文件(file2.php)上,我有以下内容:

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

执行上述代码时没有打印出来。

提前致谢

1 个答案:

答案 0 :(得分:1)

我很确定存储的会话值必须是字符串。

在file1.php中更改:

$_SESSION['answers'] = $survey_Answers;

到此:

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

然后在file2.php中更改:

$results = $_SESSION['answers'];

到此:

$results = json_decode($_SESSION['answers']);