单选按钮在 PHP 形式中恢复为第三选择


Radio Buttons Revert to Third Choice in PHP Form

我有一个严格用PHP制作的测验表格。有五组三个单选按钮。(测验中每个问题三个。一切正常,但是在单击提交按钮或刷新页面时,选中的单选按钮始终恢复为每组中的选项 3,无论用户最初选择选项 1 还是 2。如何使我的 php 在用户单击提交时保持选中状态?

$quiz = array();
$quiz['questions'] = array(
        "Which of these volcanic rocks are capable of floating?", 
        "What gemstone is Arkansas particularly known for?", 
        "What gemstone is assossiated with he month of March?",
        "Which gemstone is an alternative birthstone for June?",
        "In mythology, which of these gemstones are thought to strengthen and clense one's mind?");
$quiz['choices'][0] = array("Basalt", "Obsidian", "Pumice");
$quiz['choices'][1] = array("Diamond", "Amythist", "Arkanite");
$quiz['choices'][2] = array("Emerald", "Aquamarine", "Ruby");
$quiz['choices'][3] = array("Obsidian", "Bloodstone", "Alexandrite");
$quiz['choices'][4] = array("Emerald", "Turquoise", "Amythist");
    //Answers
    $quiz['answers'] = array('Pumice', 'Diamond', 'Aquamarine', 'Alexandrite', 'Emerald', 'submit');
foreach ($quiz['questions'] as $key => $question){
       echo "<h3>" . $question . "</h3>";
foreach($quiz['choices'][$key] as $choice){
    echo "<label>";
    if(isset($_POST[$key])){
        echo "<input type='"radio'" name='"$key'" value='"$choice'" id='"$choice'" checked> " . $choice . "<br>";
    }else{
        echo "<input type='"radio'" name='"$key'" value='"$choice'" id='"$choice'"> " . $choice . "<br>";
    }
    echo "</label>";
}}

试试这个。它对我有用

<?php
$quiz = array();
$quiz['questions'] = array(
    "Which of these volcanic rocks are capable of floating?",
    "What gemstone is Arkansas particularly known for?",
    "What gemstone is assossiated with he month of March?",
    "Which gemstone is an alternative birthstone for June?",
    "In mythology, which of these gemstones are thought to strengthen and clense one's mind?");
$quiz['choices'][0] = array("Basalt", "Obsidian", "Pumice");
$quiz['choices'][1] = array("Diamond", "Amythist", "Arkanite");
$quiz['choices'][2] = array("Emerald", "Aquamarine", "Ruby");
$quiz['choices'][3] = array("Obsidian", "Bloodstone", "Alexandrite");
$quiz['choices'][4] = array("Emerald", "Turquoise", "Amythist");
//Answers
$quiz['answers'] = array('Pumice', 'Diamond', 'Aquamarine', 'Alexandrite', 'Emerald', 'submit');
foreach ($quiz['questions'] as $key => $question) {
    echo "<h3>" . $question . "</h3>";
    foreach ($quiz['choices'][$key] as $choice) {
        echo "<label>";
        if (isset($_POST[$key]) && $_POST[$key] == $choice) {
            echo "<input type='"radio'" name='"$key'" value='"$choice'" id='"$choice'" checked> " . $choice . "<br>";
        } else {
            echo "<input type='"radio'" name='"$key'" value='"$choice'" id='"$choice'"> " . $choice . "<br>";
        }
        echo "</label>";
    }
}

编辑

您可以更改以下代码的最后foreach。它看起来更干净。

foreach ($quiz['questions'] as $key => $question) {
    echo "<h3>" . $question . "</h3>";
    foreach ($quiz['choices'][$key] as $choice) {
        echo "<label>";
        $checked = isset($_POST[$key]) && $_POST[$key] == $choice ? "checked" : "";
        echo '<input type="radio" name="' . $key . '" value="' . $choice . '" id="' . $choice . '" ' . $checked . '>' . $choice . '<br>';
        echo "</label>";
    }
}