我正在尝试构建以下形式:
<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 = '[email protected]';
$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>';
}
}
?>
但是不断出现这些错误:
注意:未定义的索引:第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行的C:\ xampp \ htdocs \ bet4info \ Index.php中提交
为什么会这样呢?
代替
$name = $_POST['name'];
if ($_POST['submit'] && $human == '4') {
使用
$name = isset($_POST['name']) ? $_POST['name'] : '';
if (isset($_POST['submit']) && $human == '4') {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句