0

这段代码的问题是我无法读取 $survey_Answers1 中的结果,除非单击两次提交按钮。

文件:survey.php

<?PHP
session_start();
    //=========================================================
    //The following page is used to create a dynamic survey.
    //=========================================================
    $qNum = 'q1';
    $question = 'Question not set';
    $answerA = 'unchecked';
    $answerB = 'unchecked';
    $answerC = 'unchecked';
    $qID = array();
    $question = array();
    $A = array();
    $B = array();
    $C = array();
    $survey_Answers = array();
    $survey_Answers1 = '';
    //============================================
    //  OPEN A CONNECTION TO THE DATABASE
    //============================================
    $user_name = "root";
    $password = "";
    $database = "surveyTest";
    $server = "127.0.0.1";
    $db_handle = mysql_connect($server, $user_name, $password);
    $db_found = mysql_select_db($database, $db_handle);
    $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 ="survey.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[$qNum])){
                $survey_Answers1 = $survey_Answers1.', '.$_POST["$qNum"];
            }
            //var_dump($survey_Answers1);
            $question_Number = ltrim($qNum,'q');
            $question_Number++;
            $qNum ='q'.$question_Number;
        }

        echo '<p>';
        //$_SESSION['answers'] = $survey_Answers1;
        //var_dump($_SESSION['answers']);
        echo "<INPUT TYPE = 'hidden' Name = 'h2'  VALUE = '".$survey_Answers1."'>";
        echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';
        var_dump($_POST['h2']);

        echo '</form>';

        mysql_close($db_handle);
    }
    else {
        print "Error getting Survey";
        mysql_close($db_handle);
    }
?>

存储在 var_dump($_POST['h2']); 中的值 点击提交按钮一次后是:字符串''(长度= 0)

PS,使用会话变量并不能解决问题,所以请不要建议这个答案!!!!

4

2 回答 2

1

尝试在for循环之后插入

for ($i = 1; $i <= 2; $i++)
{
   // your code here
}
if (empty($_POST['h2'])) $_POST['h2'] = $survey_Answers1; // insert this line

或者使用 JavaScript,插入到文件末尾:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $(function(){
        if ($('form').length > 0) {
            $('form').submit(function(e){
                var answers = '';
                $('input[type=Radio]:checked').each(function() {
                    if (answers !== '') {
                        answers += ',';
                    }
                    answers += $(this).val();
                })
                $('input[name=h2]').val(answers);

            });
        }
    })
</script>
于 2013-06-26T04:47:00.923 回答
0

从我看到的问题来看,您的 for 循环尝试查看每个循环中返回的survey_Answers1。你可以试试这个,看看每次循环和从那里分开时返回的内容

if (isset($_POST[$qNum])){
   $survey_Answers1 = $survey_Answers1.', '.$_POST["$qNum"];
?>
   <script type="text/javascript"><?php echo $survey_Answers1; ?></script>
<?php
        }
于 2013-06-26T04:24:34.863 回答