if else语句的PHP第三个分支无法正常工作

时间:2018-02-20 00:17:24

标签: php if-statement

<!doctype html>

<?php

这是用于设置cookie

//first failed attempt
if (isset($_POST['firstname']) != null || isset($_POST['lastname']) != null ||
isset($_POST['phonenumber']) != null || isset($_POST['email']) != null
 || isset($_POST['sulleyaddress']) != null || isset($_POST['question1']) != null
|| isset($_POST['question2']) != null || isset($_POST['question3']) != null
|| isset($_POST['question4']) != null || isset($_POST['question5']) != null) {
  setcookie('firstname',$_POST['firstname']);
  setcookie('lastname',$_POST['lastname']);
  setcookie('phonenumber',$_POST['phonenumber']);
  setcookie('email',$_POST['email']);
  setcookie('sulleyaddress',$_POST['sulleyaddress']);
  setcookie('question1',$_POST['question1']);
  setcookie('question2',$_POST['question2']);
  setcookie('question3',$_POST['question3']);
  setcookie('question4',$_POST['question4']);
  setcookie('question5',$_POST['question5']);

}

这是用于重置cookie

//second failed attempt
if (isset($_POST['firstname']) == null) {
  setcookie('firstname','');
}
if (isset($_POST['lastname']) == null) {
  setcookie('lastname','');
}
if (isset($_POST['phonenumber']) == null) {
  setcookie('phonenumber','');
}
if (isset($_POST['email']) == null) {
  setcookie('email','');
}
if (isset($_POST['sulleyaddress']) == null) {
  setcookie('sulleyaddress','');
}
if (isset($_POST['question1']) == null) {
  setcookie('question1','');
}
if (isset($_POST['question2']) == null) {
  setcookie('question2','');
}
if (isset($_POST['question3']) == null) {
  setcookie('question3','');
}
if (isset($_POST['question4']) == null) {
  setcookie('question4','');
}
if (isset($_POST['question5']) == null) {
  setcookie('question5','');
}

?>

<html>
  <head>
    <title>Assignment 2 - Anthony Taveras</title>
    <style>@import url("css/styles.css");</style>
    <!-- <link rel="stylesheet" href="styles.css" /> -->
  </head>
  <body>
  <?php

第1步。使表单出现。允许用户输入和提交数据。

if (!isset($_POST['submit'])) {

    ?>

    <div id="content-container">
      <div id="content">
        <form name="form" id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
          <fieldset>
            <legend>Please Fill Out the Form</legend>
            <ol>
              <li><input type="text" name="firstname" value="<?php if (isset($_COOKIE['firstname'])) {echo $_COOKIE['firstname'];}   ?>" ><label> First Name</label></li>
              <li><input type="text" name="lastname" value="<?php if (isset($_COOKIE['lastname'])) {echo $_COOKIE['lastname'];}   ?>" ><label> Last Name</label></li>
              <li><input type="text" name="phonenumber" value="<?php if (isset($_COOKIE['phonenumber'])) {echo $_COOKIE['phonenumber'];}   ?>" ><label> Phone Number</label></li>
              <li><input type="text" name="email" value="<?php if (isset($_COOKIE['email'])) {echo $_COOKIE['email'];}   ?>" ><label> Email</label></li>
              <li><input type="text" name="sulleyaddress" value="<?php if (isset($_COOKIE['sulleyaddress'])) {echo $_COOKIE['sulleyaddress'];}   ?>" ><label> Sulley Address</label></li>
            </ol>
          </fieldset>
          <fieldset>
            <legend>Please answer these questions</legend>
            <ol>
              <li class="question"><input type="text" name="question1" value="<?php if (isset($_COOKIE['question1'])) {echo $_COOKIE['question1'];}   ?>" ><label> What is your favorite color?</label></li>
              <li class="question"><input type="text" name="question2" value="<?php if (isset($_COOKIE['question2'])) {echo $_COOKIE['question2'];}   ?>" ><label> Where were you born?</label></li>
              <li class="question"><input type="text" name="question3" value="<?php if (isset($_COOKIE['question3'])) {echo $_COOKIE['question3'];}   ?>" ><label> What is your favorite food?</label></li>
              <li class="question"><input type="text" name="question4" value="<?php if (isset($_COOKIE['question4'])) {echo $_COOKIE['question4'];}   ?>" ><label> What is your favorite movie?</label></li>
              <li class="question"><input type="text" name="question5" value="<?php if (isset($_COOKIE['question5'])) {echo $_COOKIE['question5'];}   ?>" ><label> What is your favorite book?</label></li>
              <li><input type="submit" name="submit" value="Submit" /></li>
            </ol>
          </fieldset>
        </form>
      </div>
    </div>

第二步一旦提交用户就可以查看回复

  <?php 

  } elseif (isset($_POST['submit'])) {


  ?>



<!--=============================================Form Preview--------------------------------------->




    <div id="content-container">
      <div id="content">
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Here's what you put down</h1>
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First Name: <?php print $_POST['firstname']; ?> </p>
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name: <?php print $_POST['lastname']; ?> </p>
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone Number: <?php print $_POST['phonenumber']; ?> </p>
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email: <?php print $_POST['email']; ?> </p>
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sulley Address: <?php print $_POST['sulleyaddress']; ?> </p>
        <p class="question">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What is your favorite color? <?php print $_POST['question1']; ?> </p>
        <p class="question">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Where were you born? <?php print $_POST['question2']; ?> </p>
        <p class="question">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What is your favorite food? <?php print $_POST['question3']; ?> </p>
        <p class="question">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What is your favorite movie? <?php print $_POST['question4']; ?> </p>
        <p class="question">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What is your favorite book? <?php print $_POST['question5']; ?> </p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
          <input type="submit" name="edit" value="Edit" />
        </form>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
          <input type="submit" name="confirm" value="Finish" />
        </form>
      </div>
    </div>

这是问题所在。 if else语句的最后一步将无法正常工作。我不确定为什么。需要任何帮助。感谢。

  <?php } elseif (isset($_POST['confirm'])) {

  ?>




<!--=============================================Form Confirmed--------------------------------------->

    <div id="content-container">
      <div id="content">
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you, your data has been submitted</p>
      </div>
    </div>
  <?php }

   ?>



  </body>
</html>

2 个答案:

答案 0 :(得分:0)

我猜这是因为表格是空的。 尝试:

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input id='placeholder' name='placeholder' type='text' value='placeholder' hidden>
             <input type="submit" name="confirm" value="Finish" />
        </form>

答案 1 :(得分:0)

您有两个表单标签。一个是提交,一个是隐藏的领域。

因此你没有传递变量。

尝试排除故障!

echo '<pre>';
print_r($_POST);