簡體   English   中英

單選按鈕以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>";

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM