我是PHP的新手,正在从事我的第3个作业。我有一个页面,其中显示3个单选按钮,每个按钮与一个不同的调查关联。用户单击调查并提交以显示问题。我不知道为什么无论选择哪个单选按钮,都只显示第一组调查问题。我将不胜感激。
<?php
session_start();
require_once ('arrays.php');
// The isset() function is used to check whether a variable is set or not.
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
header("Location: index.php");
}
$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_FLOAT);
if ($choose_survey !== NULL) {
// Check the value of $choose_survey and then set 'survey' accordingly, e.g.
if ($choose_survey == "The Shire")
$_SESSION['survey'] = $survey0;
else if ($choose_survey == "Minas Tirith")
$_SESSION['survey'] = $survey1;
else if ($choose_survey == "Rivendell")
$_SESSION['survey'] = $survey2;
// A survey is selected so this is what happens.
// these are the survey questions
// 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);
$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['numbers'] += 1;
unset($_SESSION['survey'][$question_key]);
}
} else {
// This is what happens if there is no postback.
// add code that records which survey they selected for the results page
}
?>
<!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">
<h1 class="survey-header">Please respond to each survey statement</h1>
<?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>
**And my arrays.php file contains the following code:**
<?php
$surveys = array("The Shire", "Minas Tirith", "Rivendell");
$survey0 = array(
'Staying in the Shire was the best part of my vacation.',
'The food served at The Green Dragon tavern was excellent.',
'The party to celebrate Bilbo\'s birthday was my favorite event.',
'Gandalf\'s firework show was unforgettable experience',
'The weather during my visit was perfect.',
'The bed in Bilbo\'s home was very comfortable.',
'I enjoyed visiting The Green Dragon during my stay. ',
'I will return to the Shire for another visit in the future.',
'I would have liked my stay at the Shire to be longer.',
'Visiting the Shire has been one of the greatest experiences of my life.'
);
$survey1 = array (
'Staying in Minas Tirith was the best part of my vacation. ',
'The view at the top of Minas Tirith was the highlight of my stay.',
'Dining with the king of Minas Tirith was the highlight of my stay. ',
'The meals served in the great hall was some of the best I\'ve ever had.',
'The weather during my stay at Minas Tirith was perfect.',
'The bed in my castle suite was very comfortable.',
'I enjoyed the many royal parties in the royal courtyard.',
'I will return to Minas Tirith for another visit in the future.',
'I would have liked my stay at Minas Tirith be be longer.',
'Visiting Minis Tirith as been one of the greatest experiences of my life.'
);
$survey2 = array (
'Staying in Rivendell was the best part of my vacation.',
'Experiencing the many waterfalls was the highlight of my stay in Rivendell.',
'Attending the annual celebration with the elves in Rivendell was the highlight of my vacation.',
'The meals served in Rivendell\'s great hall was some of the best I\'ve ever had.',
'The weather during my visit in Rivendell was perfect.',
'The bed in my suite at Rivendell was very comfortable.',
'I enjoyed exploring the many beautiful gardens in Rivendell.',
'I will return to Rivendell for another visit in the future.',
'I would have liked my stay in Rivendell to be longer.',
'Visiting Rivendell has been one of the greatest experiences of my life.'
);
?>
答案 0 :(得分:2)
仅供参考,如果您创建了 Minimal, Complete, and Verifiable example ,这很难说明发生了什么,它可以帮助我们帮助您,以便我们了解问题出在哪里。
您似乎没有在检查选择了哪个调查-只需将调查设置为$survey0
。
您在做什么:
$choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_FLOAT);
if ($choose_survey !== NULL) {
// if ANY survey was selected, then set 'survey' to $survey0
$_SESSION['survey'] = $survey0;
[etc...]
您需要检查$choose_survey
的值,并用它来确定要使用的调查,例如
$choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_FLOAT);
if ($choose_survey !== NULL) {
// Check the value of $choose_survey and then set 'survey' accordingly, e.g.
if ($choose_survey == "The Shire")
$_SESSION['survey'] = $survey0;
else if ($choose_survey == "Minas Tirith")
$_SESSION['survey'] = $survey1;
else if ($choose_survey == "Rivendell")
$_SESSION['survey'] = $survey2;
[etc...]
更新:
单选按钮的值是数组索引,因此(1)将发布的数据验证为INT而不是FLOAT,并且(2)更改if语句以检查0,1和2:
// UPDATE 1: Save the posted value as an integer instead of a float:
$choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_INT);
if ($choose_survey !== NULL) {
// TEST THE VALUE: print out the saved value to see if its what you expect
echo "<p>Saved value for choose survey = $choose_survey</p>";
//Check the value of $choose_survey and then set 'survey' accordingly
// UPDATE 2: the values are the array index so change the if statements :
if ($choose_survey == 0)
$_SESSION['survey'] = $survey0;
else if ($choose_survey == 1)
$_SESSION['survey'] = $survey1;
else if ($choose_survey == 2)
$_SESSION['survey'] = $survey2;
// At this point, 'survey' should have the correct value...
[etc...]
提示:沿途打印出变量可以帮助您查看问题出在哪里。