【问题标题】:PHP Quiz not working properlyPHP测验无法正常工作
【发布时间】:2018-05-02 08:34:14
【问题描述】:

我正在尝试做一个简单的 php 测验,但我必须插入答案并将它们与 strcasecmp() 进行比较,所以如果第一个字母是大写或类似的,则不会有问题,但是代码不能正常工作。有时它不会返回正确的结果,即使我插入了正确的答案。 代码如下:

<?php
         $number = rand(1,2);
         $result = "";
         $answer = "";
         $question = "";

         $question1 = "What is the capital of China";
         $question2 = "What king of country is China?";

         $answer1 = "beijing";
         $answer2 = "republic";

         if ($number == 1) {
             $answer = $answer1;
             $question = $question1;
         }

         if ($number == 2) {
             $answer = $answer2;
             $question = $question2;
         }

         if(isset($_POST['submit'])){
            if (strcasecmp($answer,$_POST['answer']) == 0) {
                 $result = "Correct!";
            } else {
                 $result = "Wrong!";
            }
         }

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>

        <form method="post" action="index.php">
            <div class="middle">
                <p class="question"><?php echo $question; 
                ?></p>
                <p class="result"><?php echo $result;
                 $result = "" ?></p>
                 <div class="box">
                   <input type="text" name="answer" placeholder="Type here" class="text">
                 </div>
            </div>
            <input type="submit" name="submit" value="Continue" class="btn">
        </form>
    </body>
    </html>

【问题讨论】:

    标签: php strcmp


    【解决方案1】:

    通过查看您的代码,我们可以看到,当您提交表单时,您正在重新启动脚本,该脚本实际上将 $random 重置为一个新值。有 2 个问题,您有 50% 的机会得到“正确”答案,但您会发现您的脚本在添加更多问题时根本无法工作。

    基本上,您应该使用另一种方式来实现您想要的。您可以尝试在隐藏的&lt;input&gt; 中添加问题的 ID,并检查您的表单何时提交以确定它是哪一个。

    if(isset($_POST['submit'])){
       switch ($_POST['question']) { // Add '{'
       case 1:
           if (strcasecmp($answer1,$_POST['answer']) == 0) {
                $result = "Correct!";
           } else {
                $result = "Gresit!";
           }
           break;
       case 2:
            if (strcasecmp($answer2,$_POST['answer']) == 0) {
                $result = "Correct!";
           } else {
                $result = "Gresit!";
           } // Forgot to Add '}'
           break;
        } // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
    }
    

    对于 HTML,您可以在表单中添加此输入:

    <input type="text" name="question" value="<?php echo $number ?>" hidden>
    

    这不是实现您想要的最佳方式,这只是可行的示例。

    【讨论】:

    • 感谢@Gautam D 的编辑,没有看到错别字
    • 你能告诉我这个问题的想法吗?
    • 如何使用 cookie 或 $_SESSION 变量?
    • 这不是问这个的地方,因为你问了我上面回答的另一个问题。您可以使用谷歌或任何搜索引擎自己找到这个答案,因为它已经在互联网上进行了很多解释。您也可以将答案标记为已接受,因为这确实有效。您可以在 PHP php.net/manual/fr/reserved.variables.session.php 中检查以下内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多