我正在尝试做一个测验,测验已经完成了,但是有一个问题我正在尝试解决,但我自己无法解决。
我试过当我的问题从空的表格提交时它仍然会继续下一个问题。
代码:
public function getHTML()
{
self::getCurrentQuestion();
$choices = '';
foreach ($this->questions[$this->currentQuestionNumber]->giveAnswers() as $answerChoice) {
$choices .= '<input type="radio" value="' . $answerChoice->getText() . '" name="answer"/>' . $answerChoice->getText() . '<br>
';
}
$form = ' <form method="POST">
<h1>Question: ' . $this->ask[$this->currentQuestionNumber]->giveQuestionNumber() . '</h1>
<h2>' . $this->ask[$this->currentQuestionNumber]->giveText() . '</h2>
<input type="hidden" name="questions completed" value="' . htmlspecialchars(json_encode($this->questions completed)) . '">
<input type="hidden" name="counter" id="counter" value="' . $this->currentQuestionNumber . '">
'. $choices . '
<br>
<input type="submit" value="Submit">
</form>
<br>
';
return $form;
}
//the questions
$this->questions = [
new Question(1, "ork ork ork Soup do you eat with a?", [new Answer("fork", false), new Answer("spoon", true), new Answer("Knife", false), new Answer ("chopsticks", false)]),
new Question(2, "chicken soup is made of?", [new Answer("pig", false), new Answer("chicken", true), new Answer("cow", false)]),
new Question(3, "Is cheese cheese?", [new Answer("no", false), new Answer("yes", true), new Answer("Maybe", false)]),
new Question(4, "Does this new function I just created work?", [new Answer("no", false), new Answer("yes", true)]),
new Question(5, "An egg hatches from one?", [new Answer("horse", false), new Answer("chicken", true), new Answer("cow", false)]),
new Question(6, "Is the quiz working?", [new Answer("yes", true), new Answer("no", false)]),
new Question(7, "what is 200-10", [new Answer("180", false), new Answer("200", false), new Answer("190", true), new Answer("210" , false), new Answer("170", false) ])
];
我的解决方案
public function isQuestionSubmittedEmpty($question)
{
$answer = $question->getAnswer();
foreach ($answer as $answer) {
if (!empty($answer->getAnswer())) {
return false;
}
}
return true;
}
我已经尝试过 foreach 问题,如果它提交的是空的,它仍然会继续下一个问题,但也许它可以用 isset POST 函数修复。
因为每次我点击提交按钮它都会自动回到第一个问题