我有一个测验,每页显示1个问题。如果用户在未选择多项选择答案的情况下点击下一个问题,我会尝试进行验证,以便除非他们选择答案,否则他们无法前进。当用户当前按下一个问题时出现错误:'注意:未定义索引:回答'出现 有什么帮助吗?
quiz.php:
if(isset($_POST['checkQuiz'])) {
$a=$_POST['a'];
$quiz_id=$_SESSION['quiz_id'];
$index=$_SESSION['index'];
$resultQuery = mysqli_query($con,"SELECT `correctValue` FROM quiz_questions WHERE quiz_id = '$quiz_id' LIMIT 1 OFFSET $index");
$cor=0;
$incorrect=0;
while ($correct = mysqli_fetch_array($resultQuery)){
if ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
if ($_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
}
}
}
<form method="post" action="" class="form complete">
<table>
<td>
<td width = "50" id="question"><?php echo $result['question'] . "<br>"; ?></td>
</td>
<tr height = "10"></tr>
<td id= "number" width = "20" class="number"><?php echo $questionNumber ?>)</td>
<td id = "possible_answers" height = "100"width = "700">
<input type="radio" name="answer" onClick="changeColour('a')" value="<?php echo $result['answerA'] ?>"> <?php echo $result['answerA']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('b')" value="<?php echo $result['answerB'] ?>"> <?php echo $result['answerB']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('c')" value="<?php echo $result['answerC'] ?>"> <?php echo $result['answerC']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('d')" value="<?php echo $result['answerD'] ?>"> <?php echo $result['answerD']; ?> <br><br>
</table>
<?php
$_SESSION['questionNumber']=$questionNumber;
}
$a=$a+1;
?>
<input type="submit" name="exitQuiz" value="Exit Quiz" id="button1">
<?php
if ($questionNumber<$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkQuiz" value="Next Question" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
}
?>
<?php
if ($questionNumber==$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkResult" value="Quiz Result" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
} ?>
答案 0 :(得分:1)
您需要检查是否设置了$ _POST [&#39; answer&#39;],如下所示:
while ($correct = mysqli_fetch_array($resultQuery)){
if (!isset($_POST['answer']) || $_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
} elseif ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
}