我为联系表单编写代码,我不知道为什么它不起作用。一切都是用表格设置的,但我没有收到电子邮件。我没有得到任何错误或什么。这是 html代码:
<!-- Form -->
<form method="post" action="contact_form.php" name="contactform" id="contactform" class="row">
<fieldset>
<div class="form-field col-sm-6">
<label for="name">Name</label>
<span><input type="text" name="name" id="name" /></span>
</div>
<div class="form-field col-sm-6">
<label for="email">Email</label>
<span><input type="email" name="email" id="email" /></span>
</div>
<div class="form-field col-sm-6">
<label>*What is 2+2? (Anti-spam)</label>
<span><input name="human" placeholder="Type Here"></span>
</div>
<div class="form-field col-sm-12">
<label for="message">Message</label>
<span><textarea name="message" id="message"></textarea></span>
</div>
</fieldset>
<div class="form-click center col-sm-12">
<span><input type="submit" name="submit" value="Send it" id="submit" /></span>
</div>
<div id="alert" class="col-sm-12"></div>
</form>
这是 php代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'kreso.galic8@gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$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>';
}
}
?>
答案 0 :(得分:0)
似乎if ($_POST['submit']) {
无效。
您应该使用empty
或isset
:
if (!empty($_POST['submit'])) {
或
if (isset($_POST['submit'])) {
请注意,如果用户在输入字段中按Enter键而不是单击提交按钮,则不会发送提交值。
您可以使用隐藏字段:
<input type="hidden" name="ok" value="1" />