动态单选按钮 PHP
Dynamic Radio Button PHP
我目前正在创建在线测验。为了让我从输入中检索信息,我想我需要为单选按钮输入一个值。我希望第一个输入值 1,第二个输入 2,等等。因为我在 php 中使用了这个 echo,我假设它必须是某种变量?
我尝试了不同的循环,但似乎无法正常工作。抱歉,这听起来像是一个简单的问题。任何帮助,将不胜感激!
<!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>
我更改了你的代码如下:
foreach ($questionInfo['options'] as $option => $desc)
{
$key++;
echo "<input value='".$key."' type= 'radio' name = " . $questionNo . " >" . $desc . "<br>";
}
我目前正在创建在线测验。为了让我从输入中检索信息,我想我需要为单选按钮输入一个值。我希望第一个输入值 1,第二个输入 2,等等。因为我在 php 中使用了这个 echo,我假设它必须是某种变量?
我尝试了不同的循环,但似乎无法正常工作。抱歉,这听起来像是一个简单的问题。任何帮助,将不胜感激!
<!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>
我更改了你的代码如下:
foreach ($questionInfo['options'] as $option => $desc)
{
$key++;
echo "<input value='".$key."' type= 'radio' name = " . $questionNo . " >" . $desc . "<br>";
}