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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用