我有一个简单的联系表单出现在bootstrap中的模态上。出于某种原因,我的表格不会发送。过去几个小时我一直在看它,可以用一双新鲜的眼睛。这看起来很简单,但我完全失去了。
我在表单中放置了一个未显示的文本字段作为垃圾邮件过滤器。由于大多数机器人都会填充文本字段,因此我希望代码仅在文本字段为空时发送(如果用户是人类,则应该是这样,因为它不显示)。
index.php
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<a id = "btn-close" data-dismiss = "modal">X</a>
</div>
<form>
<form method="post" action="index.php">
<fieldset id= "contact-fieldset">
<input name="name" type="text" id="name" class="text-input" placeholder="First and last name" required>
<input name="email" type="email" placeholder="Email address" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<input name="bot-catch" type="text" id="bot-catch">
<input id="submit" class="submit-btn" name="submit" type="submit" value="Submit">
</fieldset>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Me';
$to = 'me@email.com';
$subject = 'Hello';
$human = $_POST['bot-catch'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '') {
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 != '') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
</form>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
编辑:所以我现在已经收到了要发送的消息,但前提是我将PHP代码放入一个单独的文件并链接到它。唯一的问题是 - 我不希望用户在发送邮件后被指示离开我的页面。有什么我能做的吗?我仍然无法弄清楚为什么它不会在我的索引页面上发送。
编辑:嗯,我搞定了!谢谢你的帮助!