我正在尝试使用一些JS验证(用于反垃圾邮件目的)在引导模式窗口中设置邮件表单。我希望窗口在提交表单后保持打开状态,并在那里显示成功/错误消息。 即使我不熟悉AJAX(我还是初学者),我已经学过一些教程并提出了一个看似可行的解决方案(相应地显示了消息,而且模态窗口没有提交后关闭),但是表单没有被发送(该站点在生产服务器上,不在本地托管)或者某处出现错误,因为我没有收到任何电子邮件。
我无法找出错误的位置。有人能帮助我吗?
这是我的代码。
HTML表单
<div class="alert alert-success hidden" id="fSuccess"><?php echo $lang['fsuccess']; ?></div>
<div class="alert alert-danger hidden" id="fError"><?php echo $lang['ferror']; ?></div>
<form id="contactForm" name="contactForm" class="form-horizontal" role="form" method="post" action="form.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label"><?php echo $lang['lname']; ?></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="<?php echo $lang['pname']; ?>" value="" required="required">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label"><?php echo $lang['lemail']; ?></label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="<?php echo $lang['pemail']; ?>" value="" required="required">
</div>
</div>
<div class="form-group">
<label for="tel" class="col-sm-2 control-label"><?php echo $lang['ltel']; ?></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="tel" name="tel" placeholder="1234567890" value="">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label"><?php echo $lang['lmessage']; ?></label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" required="required"></textarea>
</div>
</div>
<div class="form-group">
<label for="captcha" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="captcha" placeholder="<?php echo $lang['phuman']; ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" name="submit" id="submit" class="btn btn-default btn-lg"><span class="glyphicon glyphicon-send red" aria-hidden="true"></span> <?php echo $lang['send']; ?></button>
</div>
</div>
</form>
form.php的
<?php
// mail form
// salvo le variabili
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = $name;
$to = 'hidden@yahoo.it';
$subject = 'Richiesta di contatto';
$body = "Da: $name\n E-Mail: $email\n Tel: $tel\n Messaggio:\n $message";
// ok, spediamo!
mail($to, $subject, $body, $from);
}
?>
外部javascript文件
$(function () {
$('#contactForm').on('submit',function (e) {
var captcha = document.forms["contactForm"]["captcha"].value;
if (captcha == null || captcha != "5") {
$("#fError").removeClass("hidden");
return false;
} else {
$.ajax({
type: 'POST',
url: 'form.php',
data: $('#contactForm').serialize(),
success: function () {
$('#contactForm').each(function(){
this.reset();
});
$("#fError").addClass("hidden");
$("#fSuccess").removeClass("hidden");
},
error: function(){
$("#fError").removeClass("hidden");
}
});
}
e.preventDefault();
});
});