好的,我有联系表格:
<?php
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
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>';
}
}
?>
和HTML代码一起使用:
<form action="" method="POST">
<!--PHP Code above goes here-->
<div>
<label>Name</label>
<br>
<input name="name" placeholder="Type Here">
</div>
<div>
<label>Email</label>
<br>
<input name="email" type="email" placeholder="Type Here">
</div>
<div>
<label>Message</label>
<br>
<textarea name="message" placeholder="Type Here"></textarea>
</div>
<div>
<label>*What is 2+2? (Spam protection)</label>
<br>
<input name="human" placeholder="Type Here">
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
但即使填写了所有字段,它仍然告诉我填写所有必填字段。 提前谢谢。
答案 0 :(得分:1)
您需要以下列方式获取通过POST提交的所有变量的值: $ email = $ _POST ['email']
我已检查并更正了您的代码。这一切都很好,除了你必须声明POST变量:
<?php
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$name = $_POST['name'];
$email = $_POST['email'];
$human = $_POST['human'];
$from = 'example@example.com';
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == 4) {
if (mail ($email, $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>';
}
}
?>
您还应该使用isset()和!empty()函数检查表单字段是否已提交。