php - 未选择单选按钮时未定义的数组键
问题描述
未选择单选按钮时我的代码有问题(在图片上):图片在这里
这是我的代码(错误在第 123 和 124 行)
<?php
$Questions = array(
1 => array(
'Question' => '1. CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'B'
),
2 => array(
'Question' => '2. What is the Capital of the Philippines',
'Answers' => array(
'A' => 'Cebu City',
'B' => 'Davao City',
'C' => 'Manila City'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; @$counter++;
}
echo '<br /><hr>';
if (@$counter=="")
{
$counter='0';
$results = "Your score: $counter/2";
}
else
{
$results = "Your score: $counter/2";
}
} echo $results;
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
<?php } ?>
该代码有效,但是当没有选择单选按钮时,我在照片“警告:未定义的数组键”中得到错误。选择单选按钮后,一切正常。
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; @$counter++;
}
这里是123和124线。
解决方案
推荐阅读
- python - 如何从 pcap 文件中提取所有数据包的 TCP 标头?
- r - 添加一个单独的 index.html 作为主页并将 shinyapp 路由到其他页面
- android - 如何在android中每2秒解析一次来自OBD II模拟器的数据
- python - 具有相同顶级名称的不同python包
- java - 打印从 1 到用户给出的数字的整数
- php - PHP:扩展 sysvmsg
- sql - 当插入日期 > 该月的第 10 天时加载上个月的数据
- api - 无法在 itemCount 中获取数据的长度
- javascript - 如何在 Svelte 中使用 PDF.js (pdfjs-dist)
- javascript - 将从服务器返回的 json 数据分配给状态变量,然后对其进行迭代