简体   繁体   English

动态单选按钮 PHP

[英]Dynamic Radio Button PHP

I am currently creating an online quiz.我目前正在创建一个在线测验。 In order for me to retrieve information from the input, I think I am required to input a value for the radio button.为了让我从输入中检索信息,我认为我需要为单选按钮输入一个值。 I wish to input the values as 1 for the first, 2 for the second etc. Since I have used this echo in php, I am assuming it has to be some sort of variable?我希望将值输入为 1 用于第一个,2 用于第二个等。由于我在 php 中使用了这个 echo,我假设它必须是某种变量?

I have tried different loop but I can't seem to get it working.我尝试了不同的循环,但我似乎无法让它工作。 Sorry if this sounds like an easy question.对不起,如果这听起来像一个简单的问题。 Any help would be appreciated!任何帮助,将不胜感激!

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <title>Online Quiz</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/styles.css">  
</head>
<body>
    <div>
        <form action="" method="post" id="quiz">
            <?php
            require'./quizData.php';
            include('Header.php');

            foreach ($quizSGHistoryQ as $questionNo => $questionInfo) {
                echo "<br>" . $questionNo . ")";
                echo $questionInfo['question'];
                echo $questionInfo['answer'] . "<br>";
                foreach ($questionInfo['options'] as $option => $desc) {
                        echo "<input type= 'radio' name = " . $questionNo  .      " >" . $desc . "<br>";
                }
            }
            include('Footer.php');
            ?>
            <input type="submit" name="submit" value="Submit">
       </form>
    </div>
    <?php
    foreach ($quizSGHistoryQ as $questionNo => $questionInfo) {
        $pickedAns = $_POST [$questionNo];
        $totalScore = 0;
        if ($pickedAns == $questionInfo['answer']) {
           $totalScore++;
        }
    }
    ?> 
</body>
</html>

i changed your code as follow:我将您的代码更改如下:

foreach ($questionInfo['options'] as $option => $desc) 
{
   $key++;
   echo "<input value='".$key."' type= 'radio' name = " . $questionNo  .      " >" . $desc . "<br>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM