dtng25909 2013-06-26 03:44
浏览 18
已采纳

需要单击提交按钮两次以从PHP脚本中的单选按钮获取结果

The problem with this code is that I can't read the results in $survey_Answers1 except after clicking on the submit button twice.

file: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);
    }
?>

The value stored in var_dump($_POST['h2']); after hitting clicking the submit button once is: string '' (length=0)

P.S., using session variables doesn't resolve the problem, so please don't suggest this answer!!!!

  • 写回答

2条回答 默认 最新

  • douyingmou1389 2013-06-26 04:47
    关注

    Try to insert after for-loop

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

    Or use JavaScript, insert to the end of file:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?