【问题标题】:Radio Buttons Revert to Third Choice in PHP FormPHP 表单中的单选按钮恢复为第三选择
【发布时间】:2015-03-06 14:26:41
【问题描述】:

我有一个严格使用 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 forms radio-button


    【解决方案1】:

    试试这个。它对我有用

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

    【讨论】:

    • 我不能在我的文档中使用它,因为它是一个 php html 混合。我上面的代码是从可靠的 php 中提取的。
    • 不。单选按钮被完全消除。不过,问题仍然存在。
    • 你能在第一个foreach之前请print_r($quiz);,这样我就可以看到数组的样子了
    • 知道了。您只是在检查是否设置了 POST[$key]。你需要比较POST[$key]是否等于$choice
    • 你在 foreach 循环之外有什么改变吗?因为现在我没有检查收音机。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2011-08-31
    • 2014-03-07
    • 2012-12-14
    • 2015-11-16
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多