IF语句的结果显示不正确

时间:2019-07-05 16:25:46

标签: php

知道我的代码有什么问题吗? 如果变量的值正确且输出正确,则表明它不正确。

然后尝试多次,结果是正确的,一切都完美了。

此后,我再次尝试执行代码,单击“刷新”时,原来的结果仍然存在。

输入与值变量不匹配,但是仍然显示旧结果,它是正确的。

感谢所有帮助

南希

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Printing Quotes</title>
</head>
<body>
    <div class='container'>
        <header>
            <h1 id="title">Who said this quote?</h1>
        </header>
        <section>
            <?php
                $quote="These aren't the droids you're looking for.";
                $author = 'Kenobi';

                echo "<div id='question'>
                        <h2>$quote</h2>
                      </div>";

            ?>
                <form action="" method="POST">
                    <input id='input' type="text" name="author" placeholder="Enter your answer">
                </form>
        </section>
        <section>
            <?php
                if(isset($_POST['author']) == $author){

                    echo "<div id='correct'> Correct!!!</div>";

                    echo "<br>";

                    echo 
                    "<div id='quote'>{$author} says: \" $quote \" </div>";

                    echo 'match';        
                    echo '<br>';
                    echo 'Variable value:'. $author;
                    echo '<br>';
                    echo 'User Input:'. $_POST['author'];

                }

                else {
                    echo "not match";
                    echo '<br>';
                    echo 'Variable value:'. $author;
                    echo '<br>';
                    echo 'User Input: '. $_POST['author'];

                }
            ?>
        </section>
    </div>

</body>
</html>

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

isset($_POST['author'])是布尔检查。您正在将TRUE | FALSE与$author的内容进行比较。我怀疑您是要放if(isset($_POST['author']) && $_POST['author'] == $author)

答案 1 :(得分:2)

如果我目前了解您,如果您将表单发送到自己的php文件,请在表单操作中使用$ _SERVER ['PHP_SELF']然后检查

if(isset($_POST['author']) && $_POST['author'] == $author)