PHP 如何以绿色打印所选答案

PHP How to print selected answer in green

这类似于我之前询问的关于将 selected 单选按钮与输入字段中的值匹配的问题。

该代码允许用户通过输入问题和 4 个可能的答案来构建测验。您必须 select 一个答案是正确的,并且在单独的 PHP 页面上,答案必须以绿色显示正确的答案。本来这个问题解决了,但是我修改验证代码后,原来的显示循环就不行了。

这是代码的输出:

我需要 Albany 以绿色打印,因为它是正确答案并且 select 使用单选按钮编辑。

这是我的表单代码:

<?php
    session_start();

    // Define variables and set to empty values
    $questionErr = $answer0Err = $answer1Err = $answer2Err = $answer3Err = "";
    $question = $answer0 = $answer1 = $answer2 = $answer3 = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $valid = True;

        if (empty($_POST['question'])) {
            $questionErr = "Please supply a question";
            $valid = False;
        } else {
            $question = test_input($_POST['question']);
            $_SESSION['question'] = $_POST['question'];
        }

        if (empty($_POST['answer0'])) {
            $answer0Err = "Please supply a possible answer";
            $valid = False;
        } else {
            $answer0 = test_input($_POST['answer0']);
            $_SESSION['answer0'] = $_POST['answer0'];
        }

        if (empty($_POST['answer1'])) {
            $answer1Err = "Please supply a possible answer";
            $valid = False;
        } else {
            $answer1 = test_input($_POST['answer1']);
            $_SESSION['answer1'] = $_POST['answer1'];
        }

        if (empty($_POST['answer2'])) {
            $answer2Err = "Please supply a possible answer";
            $valid = False;
        } else {
            $answer2 = test_input($_POST['answer2']);
            $_SESSION['answer2'] = $_POST['answer2'];
        }

        if (empty($_POST['answer3'])) {
            $answer3Err = "Please supply a possible answer";
            $valid = False;
        } else {
            $answer3 = test_input($_POST['answer3']);
            $_SESSION['answer3'] = $_POST['answer3'];
        }
    }

    // Function to sanitize data
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    // If valid, send to QuestionReview.php to display answers
    if ($valid) {
        $_SESSION['radio'] = $_POST['radio'];
        header('location: QuestionReview.php');
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>User-Created Quiz</title>
        <style>
            .shadow {
                 -webkit-box-shadow: 3px 3px 5px 6px #ccc;  /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
                 -moz-box-shadow:    3px 3px 5px 6px #ccc;  /* Firefox 3.5 - 3.6 */
                 box-shadow:         3px 3px 5px 6px #ccc;  /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */
            }
            .instructions {
                color: #696D6E;
            }
            #form-background {
                background-color: #ECEDE8;
            }
            .error {
                color: red;
            }   
        </style>
    </head>
    <body>
        <div style="width:600px">

        <fieldset id="form-background" class="shadow">
            <h1 class="instructions" style="text-align:center">User-Created Quiz</h1>
            <p class="instructions" style="text-align:center">
            <strong>Please enter a question of your own, 
                    along with 4 possible answers in the 
                    form below. Be sure to select the 
                    correct answer to your question
                    before submitting the form.</strong>
            </p>
            <form style="text-align:center;" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

                <br>
                <label class="instructions" for="question" >Enter your question here</label><br>
                <input type="text" name="question" size="50" value='<?php echo $question;?>' />
                <span class="error">* <br /><?php echo $questionErr; ?></span>
                <br><br>
                <p class="instructions">
                    Please provide four answers to your question and select the 
                    correct one.
                </p>
                    <input type="radio" name="radio" value="answer0">
                    <input type="text" name="answer0" value="<?php echo $answer0; ?>" style="width:400px">
                    <span class="error">* <br /><?php echo $answer0Err; ?></span>
                    <br><br>

                    <input type="radio" name="radio" value="answer1">
                    <input type="text" name="answer1" value="<?php echo $answer1; ?>" style="width:400px">
                    <span class="error">* <br /><?php echo $answer1Err; ?></span>
                    <br><br>

                    <input type="radio" name="radio" value="answer2">
                    <input type="text" name="answer2" value="<?php echo $answer2; ?>" style="width:400px">
                    <span class="error">* <br /><?php echo $answer2Err; ?></span>
                    <br><br>

                    <input type="radio" name="radio" value="answer3">
                    <input type="text" name="answer3" value="<?php echo $answer3; ?>" style="width:400px">
                    <span class="error">* <br /><?php echo $answer3Err; ?></span>
                    <br><br>

                    <input type="submit" value="Submit Entry">
            </form>
        </fieldset>
        </div>

    </body>
</html>

这是我的结果页面代码:

<?php
    session_start();

    // Pull all variables from SESSION
    $question =  $_SESSION['question'];
    $answer0 = $_SESSION['answer0'];
    $answer1 = $_SESSION['answer1'];
    $answer2 = $_SESSION['answer2'];
    $answer3 = $_SESSION['answer3'];
    $radio = $_SESSION['radio'];

    $answerArray = array($answer0, $answer1, $answer2, $answer3);
    shuffle($answerArray);
?>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Entry Review</title>
        <style>
            .instructions {
                color: #696D6E;
            }
        </style>
    </head>
    <body>
        <h1 class="instructions">Entry Review</h1>
        <p><em>You entered the following question:</em></p>
        <p><strong><?php echo $question; ?></strong></p><br>



        <p><em>These are the answers you provided:</em>
        <p>
            <strong>
                <?php 

                if(isset($_SESSION['radio'])) {
                    $radio =  $_SESSION['radio'];
                    foreach ($answerArray as $value) {
                        echo $value . "<br />";
                    }
                } else {
                    echo "Please click the back button in your browser and select a correct answer";
                }
                ?>
            </strong>
        </p>
    </body>
</html>

拿一张 A4 普通纸,离开电脑工作十分钟。

现在,确定您要在块中实现的目标,您希望 skillset/language/operator-syntax 的每个部分是什么(PHP、javascript、CSS, HTML 等等)要做什么?

您的项目中已经显示了一个输出列表,并且您想要标记一个选定的输出,使其与其他输出不同。

从最终结果向后推算,是什么导致了这种变化?没错,CSS,所以你可以在纸上写下,你需要一个CSSclass(或其他标识符)来选择输出,你可以要求那个答案。

那么 CSS 如何知道选择哪个输出?这些数据保存在哪里?

此数据似乎在 PHP 某处,(但从您的代码中不能立即看出)。因此,您需要一个 PHP IF 语句来检查 如果正在显示给浏览器的答案是用户的答案chosen 那么这个答案需要以某种方式(<div><span> )包含在 CSS class 中以影响外观差异。

就是这样。你现在应该有足够的结构去离开并在 A4 sheet 上写下笔记,然后使用这些笔记将你的各种问题分解成组成部分(这里有 确定 应该发生什么行为,然后使 发生该行为)。

在 5-10 分钟内重写您的代码,您将完全按照自己的意愿获得它。

你快完成了...简单点:

如果你有:

$question =  $_SESSION['question'];
$radio = $_SESSION['radio'];
$answerArray = [$_SESSION['answer0'], $_SESSION['answer1'], $_SESSION['answer2'], $_SESSION['answer3']];

那么您可以:

if(isset($_SESSION['radio'])) {
  foreach ($answerArray as $value) {
    echo $_SESSION['radio'] == $value ? "<span style=\"color:green\">Make $value green.</span><br />" : "<span style=\"color:#666\">Make $value grey or red.</span><br />";
  }
} else {
  echo "Please click the back button in your browser and select a correct answer";
}

等于:

if(isset($_SESSION['radio'])) {
  foreach ($answerArray as $value) {
    if ($_SESSION['radio'] == $value){
      echo "<span style=\"color:green\">Make $value green.</span><br />";
    } else {
      echo "<span style=\"color:#666\">Make $value grey or red.</span><br />";
    }
  }
} else {
  echo "Please click the back button in your browser and select a correct answer";
}

QuestionReview.php页面上,得到这样的正确答案,

$correct_answer = $_SESSION[$_SESSION['radio']];

因此请保持您的测验页面不变,并在提交后按如下方式处理您的表单:

QuestionReview.php:

<?php
    session_start();

    // Pull all variables from SESSION
    $question =  $_SESSION['question'];
    $answer0 = $_SESSION['answer0'];
    $answer1 = $_SESSION['answer1'];
    $answer2 = $_SESSION['answer2'];
    $answer3 = $_SESSION['answer3'];
    $correct_answer = $_SESSION[$_SESSION['radio']];

    $answerArray = array($answer0, $answer1, $answer2, $answer3);
    shuffle($answerArray);

?>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Entry Review</title>
        <style>
            .instructions {
                color: #696D6E;
            }

        </style>
    </head>
    <body>
        <h1 class="instructions">Entry Review</h1>
        <p><em>You entered the following question:</em></p>
        <p><strong><?php echo $question; ?></strong></p><br>



        <p><em>These are the answers you provided:</em>
        <p>
            <strong>
                <?php 

                if(isset($_SESSION['radio'])) {
                    foreach ($answerArray as $value) {
                        $output = "<span";
                        if($value == $correct_answer){
                            $output .= " class='instructions'";
                        }
                        $output .= ">" . $value . "</span><br />";
                        echo $output;
                    }
                } else {
                    echo "Please click the back button in your browser and select a correct answer";
                }
                ?>
            </strong>
        </p>
    </body>
</html>

foreach循环的每次迭代中,检查当前选项是否等于正确答案。如果是,则将 class instructions 应用于当前选项。

输出:(截图)