回答两个问题以继续


Answering two questions to proceed further

我试图构建一个简单的php页面,用户在其中回答两个问题以继续操作。我的代码是:

<?php
if (!isset($_POST['answer1']) OR $_POST['answer1'] != "blue")
   {
   ?>
    <form action="index.php" method="post">
    <p>What color is the sky ?</p>
    <input type="text" name="answerswer1">
    <input type="submit" value="Send">
    </p>
   <?php }
   else {
      if (!isset($_POST['answer2']) OR $_POST['answer2'] != "white")
    {
    ?>
       <form action="index.php" method="post">
        <p>What color is the milk ?</p>
        <input type="text" name="answerswer2">
        <input type="submit" value="Send">
        </p>
    <?php }
    else {
        echo 'Congratulation !';
    }
  }

?>

当回答第一个问题时,显示第二个问题。但当回答第二个问题时,我又回到了第一个问题,而没有显示祝贺信息。有人能告诉我出了什么问题吗?提前谢谢。

您的脚本永远不会到达else块,因为变量值不是永久存储的。您需要使用会话,或者引入不同的方法来解决此问题。

如果你想用一个页面来做这件事,你可以在同一页面中添加两个输入字段,然后只有在两个答案都正确的情况下才转发用户。

在我看来,最好的方法是使用captcha。

你的错误很常见,是一个逻辑错误:

在第一个IF语句中,您询问POST应答1是否存在或是否为蓝色。

但是。。当你以第二种形式提交请求时,该请求没有设置答案1,所以。。回到第一个if。。

您的解决方案很简单:

 else {
  if (!isset($_POST['answer2']) OR $_POST['answer2'] != "white")
{
?>
   <form action="index.php" method="post">
    <p>What color is the milk ?</p>
    <input type="hidden" name="answerswer1" value="blue"> <--- add this line to skip the first if uin the second submit
    <input type="text" name="answerswer2">
    <input type="submit" value="Send">
    </p>
<?php }
else {
    echo 'Congratulation !';
}

这很有效只需在第二个表单中添加一个隐藏字段

<?php
if (!isset($_POST['answer1']) OR $_POST['answer1'] != "blue")
   {
   ?>
    <form action="index.php" method="post">
    <p>What color is the sky ?</p>
    <input type="text" name="answerswer1">
    <input type="submit" value="Send">
    </p>
   <?php }
   else {
      if (!isset($_POST['answer2']) OR $_POST['answer2'] != "white")
    {
    ?>
       <form action="index.php" method="post">
        <p>What color is the milk ?</p>
        <input type="hidden" name="answerswer1" value="blue">
        <input type="text" name="answerswer2">
        <input type="submit" value="Send">
        </p>
    <?php }
    else {
        echo 'Congratulation !';
    }
  }
?>

如果您不想更改表单结构,请更改If/else子句:

<?php 
    if(isset($_POST['answer2']) && $_POST['answer2'] == "white")
    {
        echo 'Congratulation !';
    } else if((isset($_POST['answer1'] && $_POST['answer1'] == "blue") || (isset($_POST['answer2'] && $_POST['answer2'] != "white"))
    { ?>
    <form action="index.php" method="post">
        <p>What color is the milk ?</p>
        <input type="text" name="answerswer2">
        <input type="submit" value="Send">
    </form>
    <?php } else { ?>
    <form action="index.php" method="post">
        <p>What color is the sky ?</p>
        <input type="text" name="answerswer1">
        <input type="submit" value="Send">
    </form>
    <?php } ?>

更改此项:

!isset($_POST['answer1']) OR $_POST['answer1'] != "blue"

到此:

(!isset($_POST['answer1']) OR $_POST['answer1'] != "blue") && !isset($_POST['answer2'])

添加answer2作为不在第一个条件中设置的要求,将使其在提交第二个表单时传递给下一个条件。

您总是可以传递第三个检查变量,例如:

if(isset($_POST['completed'] && $_POST['completed'] == true){ 
    echo "Congratulations";
    exit();
}

把它放在其他语句之上,这样它就是第一个被检查的东西。