【发布时间】:2015-08-26 20:10:25
【问题描述】:
我正在尝试构建以下表单:
<form method="post" action="Index.php">
<label>Name </label>
<input name="name" placeholder="Type Here"><br />
</br>
<label>Email </label>
<input name="email" placeholder="Type Here">
<br /></br>
<label style="display:block;">I need some information regarding:</label>
<textarea name="message" placeholder="Type Here"></textarea>
<br />
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<br />
<input id="submit" name="submit" type="submit" value="Submit" style="height:30px;">
</form>
PHP 代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'hello@emailaddress.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
但不断出现这些错误:
注意:未定义的索引:C:\xampp\htdocs\bet4info\Index.php 中的名称 第 19 行
注意:未定义索引:C:\xampp\htdocs\bet4info\Index.php 中的电子邮件 第 20 行
注意:未定义索引:C:\xampp\htdocs\bet4info\Index.php 中的消息 第 21 行
注意:未定义索引:C:\xampp\htdocs\bet4info\Index.php 中的人类 第 25 行
注意:未定义索引:在 C:\xampp\htdocs\bet4info\Index.php 中提交 第 29 行
注意:未定义索引:在 C:\xampp\htdocs\bet4info\Index.php 中提交 第 35 行
注意:未定义索引:在 C:\xampp\htdocs\bet4info\Index.php 中提交 第 38 行
为什么会这样?
【问题讨论】:
-
因为您从 $_POST 设置为示例名称,并且在检查值为值之前不要使用 isset 函数
-
这对我有用,但您需要在
$_POST值上应用isset()。 -
@anantkumarsingh 错了。而且,由于?您需要在您认为无法初始化的每个 var 上使用 isset,例如 php 建议,不一定在 $_POST 上,但仅作为示例在 $name 上(在 OP 情况下)
-
如果你给定的代码在一个文件中,你肯定会得到这些错误。
-
我说 $_POST 值,表示他需要检查的每个值。请完整阅读我的评论。