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

时间:2015-03-06 02:50:56

标签: php forms radio-button

我有一个严格用PHP制作的测验表格。有五组三个单选按钮。 (测验中每个问题三个。)一切正常,但点击提交按钮或刷新页面后,无论用户最初是否选择了选项1,选中的单选按钮始终会恢复为每个选项中的选项3或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>";

}}

1 个答案:

答案 0 :(得分:0)

试试这个。它对我有用

<?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>";

    }
}