簡體   English   中英

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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM