PHP未识别索引,单独的PHP和html文件


PHP Unidentified index, separate php and html files

我知道这个话题已经被触及了很多,但一个解决方案仍然在回避我。我在这个php文件中不断收到一个未识别的索引错误:

<?php
  $name = isset($_POST['name']) ? $_POST['name'] : '';
  $telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
  $email = isset($_POST['email']) ? $_POST['email'] : '';
  $birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
  $gender = isset($_POST['gender']) ? $_POST['email'] : '';
  $comments = isset($_POST['comments']) ? $_POST['comments'] : '';
?>
Thanks for submitting your application!<br>
The following is the information we received:<br>
Name: <?php echo $_POST['name'] ?><br>
Telephone: <?php echo $_POST['telephone']?><br>
E-Mail: <?php echo $_POST['email']?><br>
Birthday: <?php echo $_POST['birthDate '] ?><br>
Gender: <?php echo $_POST['gender'] ?><br>
When you first wanted to be a zookeeper: <?php echo $_POST['comments '] 
?>
</body>
</html>

以下是这个php文件从中获取值的html表单:

<form id="zooKeeperForm" action="zoo.php" method="GET" onsubmit="return validateForm()">
  <p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
  <fieldset>
    <legend>Contact Details</legend>
      <label for="name">Name <em>*</em></label>
      <input id="name" placeholder="Jane Smith" autofocus required><br>
      <label for="telephone">Telephone</label>
      <input id="telephone" placeholder="(xxx) xxx-xxxx"><br>
      <label for="email">Email <em>*</em></label>
      <input id="email" type="email" required><br>
  </fieldset>
  <fieldset>
    <legend>Personal Information</legend>
      <label for="birthDate">Birth Date<em>*</em></label>
      <input id="birthDate" type="date" required><br>
      <label for="age">Age<em>*</em></label>
      <input id="age" type="number" min="0" max="120" step="0.1" required><br>
      <label for="gender">Gender</label>
      <select id="gender">
        <option value="female">Female</option>
        <option value="male">Male</option>
      </select><br>
      <label for="comments">When did you first know you wanted to be a zoo-keeper?<em>*</em></label>
      <textarea id="comments" oninput="validateComments(this)" required></textarea>
  </fieldset>
  <p><input type="submit" value="Submit Application"></p>
</form>

我在这里做错了什么??我觉得自己像个白痴。我一直在获取未识别的索引错误,直到它打印出我想要的格式,但对于所有应该插入的值来说,它都是空白的。

第一件事:

将其更改为POST

<form id="zooKeeperForm" action="zoo.php" method="POST" onsubmit="return validateForm()">
                                             //  ^ POST not GET

第二。名称属性是要使用的属性,而不是ID

<input id="name" placeholder="Jane Smith" autofocus required>
      // NOT ID but name="name"

这必须是:

<input name="telephone" placeholder="(xxx) xxx-xxxx" id="telephone" />
<input name="email" type="email" required id="email" />
<input name="birthDate" type="date" required id="birthDate" />
<select name="gender" id="gender">
<textarea name="comments" oninput="validateComments(this)" id="comments" required>

第三:

这里的简单之处在于,始终处理表单输入表单提交。不在初始负载时。

用这样的东西捕捉提交:

<input type="submit" name="zoo_submit" value="Submit Application" />

然后在PHP中:

// simple initialization
$name = $telephone = $email = $birthDate = $gender = $comments = '';
if(isset($_POST['zoo_submit'])) {
  $name = isset($_POST['name']) ? $_POST['name'] : '';
  $telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
  $email = isset($_POST['email']) ? $_POST['email'] : '';
  $birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
  $gender = isset($_POST['gender']) ? $_POST['email'] : '';
  $comments = isset($_POST['comments']) ? $_POST['comments'] : '';
}
?>
Thanks for submitting your application!<br>
The following is the information we received:<br>
<!-- now you have already set the variables above, use them, not the POST values again -->
Name: <?php echo $name; ?><br>
Telephone: <?php echo $telephone; ?><br>
E-Mail: <?php echo $email; ?><br>
Birthday: <?php echo $birthDate; ?><br>
Gender: <?php echo $gender; ?><br>
When you first wanted to be a zookeeper: <?php echo $comments; ?>