首页 > 解决方案 > PHP 从数组中选择一个调查

问题描述

我是 PHP 的新手,正在处理我的第三个任务。我有一个页面显示 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.'
    );

?>

标签: php

解决方案


仅供参考,很难说出发生了什么,如果您创建一个最小、完整和可验证的示例,它可以帮助我们帮助您,这样我们就可以看到问题出在哪里。

看起来您并没有检查选择了什么调查 - 您只是将调查设置为$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...]

提示:沿途打印出变量可以帮助您了解问题发生在哪里。


推荐阅读