我是PHP的新手,正在努力完成这项工作。我早些时候发布了类似的问题,但是我遗漏了部分代码。我做了更多的工作,但仍然无法正常工作。用户选择调查后,将向他们提供10个问题的序列。他们必须选择一个答案(单选按钮),然后单击提交,将他们带到下一个问题。现在,用户可以浏览所有问题,在每个问题之后单击提交,而无需选择答案。我知道我需要验证是否已为每个问题选择答案(单选按钮)。我不明白我在做什么错。我已经阅读了数十篇解释验证的文章,但这项工作涉及很多部分。我不知道在哪里包括我的验证。帮助将不胜感激。我知道我只是想包括相关的尝试代码,但是现在我不确定代码的哪些部分对于呈现我的问题至关重要。如果我加入过多内容,我深表歉意。
$isPostBack = filter_input(INPUT_POST, 'submitButton') !== NULL;
if ($isPostBack) {
// This is what happens if there is a postback.
$choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_INT);
if ($choose_survey !== NULL) {
// Check the value of $choose_survey and then set 'survey' accordingly, e.g.
if ($choose_survey == 0)
$_SESSION['survey'] = $survey0;
else if ($choose_survey == 1)
$_SESSION['survey'] = $survey1;
else if ($choose_survey == 2)
$_SESSION['survey'] = $survey2;
// A survey is selected so this is what happens.
// these are the survey questions
/* $_SESSION['survey'] = $survey0;*/
// this will contain the answers
$_SESSION['answers'] = array();
// this is the question number
$_SESSION['number'] = 1;
} else {
// A survey is not selected because it was already chosen.
// get the value from the radio button.
$answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
if ($answer !== NULL) {
$_SESSION['answers'] = $answer;
} else if ($answer == NULL) {
echo '<p>Please selection an answer</p>';
}
// Stop user from submitting survey question that hasn't been answered
$question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_FLOAT);
// this will be used later to display the answers/results
$_SESSION['answers'][$question_key] = $answer;
// This is adding 1 to the question number.
$_SESSION['number'] += 1;
unset($_SESSION['survey'][$question_key]);
}
} else
?>
<!DOCTYPE html>
<html lang="en">
<body>
<form id="survey" name="survey" method="post" action="survey.php">
<?php
if (!isset($_SESSION['survey'])):
echo '<p>Please choose a survey</p>';
foreach ($surveys as $key => $value) {
$surveyButton = <<<HEREDOC
<label for = "$key">$value</label>
<input type="radio" name="choose_survey" id="$key" value="$key"><br>
HEREDOC;
echo $surveyButton;
}
else: ?>
<div class="wrap">
<p class="survey-header">Please respond to each survey statement</p>
<?php
$key = array_key_first($_SESSION['survey']);
$value = $_SESSION['survey'][$key];
$surveyQuestions = <<<HEREDOC
<input type="hidden" name="question_key" value="$key">
<label class="statement"> $value </label>
<ul class='likert'>
<li>
<input type="radio" id="$key" name="answer" value="strong_agree">
<label>Strongly agree</label>
</li>
<li>
<input type="radio" id="$key" name="answer" value="agree">
<label>Agree</label>
</li>
<li>
<input type="radio" id="$key" name="answer" value="neutral">
<label>Neutral</label>
</li>
<li>
<input type="radio" id="$key" name="answer" value="disagree">
<label>Disagree</label>
</li>
<li>
<input type="radio" id="$key" name="answer" value="strong_disagree">
<label>Strongly disagree</label>
</li>
</ul>
HEREDOC;
echo $surveyQuestions;
?>
</div>
<?php endif; ?>
<br/>
<input type="submit" name="submitButton" value="Submit">
</form>
</body>
</html>
答案 0 :(得分:1)
我认为您要比现在更难做。
$answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
if ($answer !== NULL) {
$_SESSION['answers'] = $answer;
} else if ($answer == NULL) {
echo '<p>Please selection an answer</p>';
}
// Stop user from submitting survey question that hasn't been answered
$question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_FLOAT);
// this will be used later to display the answers/results
$_SESSION['answers'][$question_key] = $answer;
// This is adding 1 to the question number.
$_SESSION['number'] += 1;
unset($_SESSION['survey'][$question_key]);
替换为
$answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
if (!$answer) { //because your answers are truthy, no empty values like '0'
echo '<p>Please selection an answer</p>';
}else{
$_SESSION['answers'] = $answer;
$question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_FLOAT);
// this will be used later to display the answers/results
$_SESSION['answers'][$question_key] = $answer;
// This is adding 1 to the question number.
$_SESSION['number'] += 1;
unset($_SESSION['survey'][$question_key]);
}
您已经知道它是否已回答,您只是没有正确构造代码。在您的代码中,该代码仅在条件之后继续,而在我的代码中,如果$answer
我不为空,则仅执行该代码。