我知道这个话题已经被触及了很多,但一个解决方案仍然在逃避我。我一直在这个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>
我在这做错了什么?我觉得自己像个白痴。我不断将未识别的索引错误发送到打印出我想要的格式的位置,但是对于应该插入的所有值,它只是空白。
答案 0 :(得分:2)
第一件事:
将其更改为POST
<form id="zooKeeperForm" action="zoo.php" method="POST" onsubmit="return validateForm()">
// ^ POST not GET
二。名称属性是要使用的属性而不是ids
<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; ?>