dongyu8694 2015-04-17 06:26
浏览 36
已采纳

使用PHP动态创建和填充输入无线电

I'm currently working with PHP and HTML, I'm making a multiple choise exam, the PHP file contains four arrays, the first one storage the questions, the second one storages the probable answers for option "a)", the third one storages the probable answers for option "b)" and finally the last one storages the answers for option "c)"

Here his the code to create and populate both the questions and the multiple choises.

for($i=0; $i<20; $i++){
    echo "$i.".$preguntas[$i]."<BR>";
    echo"<input type = 'radio'  name='R$i'>a)".$r1[$i]."<br>";
    echo"<input type = 'radio'  name='R$i'>b)".$r2[$i]."<br>";
    echo"<input type = 'radio'  name='R$i'>c)".$r3[$i]."<br>";
    echo "<BR><BR>";

}  

I'm having some troubles to make this work, any idea how coud I grade the correct answers

Thanks in advance

  • 写回答

3条回答 默认 最新

  • dongzhao8233 2015-04-17 08:34
    关注
    <html>
    <body>
    <form method="post">
    <?php
    echo "<pre>";
    var_export($_POST['R']);
    echo "</pre>";
    for($i=0; $i<20; $i++){
        echo "$i.".$preguntas[$i]."<BR>";
        echo"<input type='radio' name='R[$i]' value='a'>a){$r1[$i]}<br>";
        echo"<input type='radio' name='R[$i]' value='b'>b){$r2[$i]}<br>";
        echo"<input type='radio' name='R[$i]' value='c'>c){$r3[$i]}<br>";
        echo "<BR><BR>";
    }
    ?>
    <button>submit</button>
    </form>
    </body>
    </html>
    

    You'll receive something like:

    array (
      0 => 'a',
      1 => 'b',
      2 => 'b',
    )
    

    For example:

    <html>
    <body>
    <form method="post">
    <?php
    $questions = [
        [
            'question' => 'Who was first programmer?',
            'answers' => [
                'a' => 'Alan Turing',
                'b' => 'Ada Lovelace',
                'c' => 'Rasmus Lerdorf',
                'd' => 'James Bond',
            ],
            'correctAnswer' => 'b',
        ],
        [
            'question' => 'Who created php?',
            'answers' => [
                'a' => 'Alan Turing',
                'b' => 'Ada Lovelace',
                'c' => 'Rasmus Lerdorf',
            ],
            'correctAnswer' => 'c',
        ],
        [
            'question' => 'Who created Turing machine?',
            'answers' => [
                'a' => 'David Beckham',
                'b' => 'Rasmus Lerdorf',
                'c' => 'Floyd Mayweather, Jr.',
                'd' => 'Ada Lovelace',
                'e' => 'Alan Turing',
            ],
            'correctAnswer' => 'e',
        ],
    ];
    if (!empty($_POST['response'])) {
        foreach ($_POST['response'] as $questionId => $answerKey) {
            echo '<h5>'.$questions[$questionId]['question'].'</h5>';
            if ($questions[$questionId]['correctAnswer'] === $answerKey) {
                echo 'Correct answer.<br>';
            } else {
                echo 'Wrong answer.<br>';
            }
        }
    } else {
        foreach ($questions as $questionId => $data) {
            echo '<h5>'.$data['question'].'</h5>';
            foreach ($data['answers'] as $key => $answer) {
                echo '<input type="radio" name="response['.$questionId.']" value="'.$key.'">'.$key.') '.$answer.'<br>';
            }
            echo "<br><br>";
        }
    }
    ?>
    <button>submit</button>
    </form>
    </body>
    </html>
    

    You can tweak it here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么