我是一个初学者,但我已经为我的网站整理了一份反馈表格。我遇到的问题是表单发送了2封电子邮件,而不是1封。你有什么想法吗?
$('#myForm')
.on('valid.fndtn.abide', function () {
var name = $("input#name").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
var dataString = 'name=' + name +
'&email=' + email +
'&message=' + message;
$.ajax({
type: "POST",
url: "/contact/mail.php",
data: dataString,
success: function() {
$('.contactform').html("<div id='thanks'></div>");
$('#thanks').html("<h2>Thanks!</h2>")
.append("<p>Dear "+ name +"!, We will get back to you as soon as we can.</p>")
.hide()
.fadeIn(1500);
},
}); //ajax call
return false;
});
PHP:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$msg = "
Name: $name
Email: $email
Comments:
$message
";
$to = "email@example.com";
$subject = "Website";
$message = $msg;
$headers = "Website";
mail($to,$subject,$message,$headers);
?>
显然,Foundation 5仍然包含早期版本"valid"与"valid.fndtn.beart"中的一个旧调用函数,这导致它被调用两次。我能够通过进行以下更改来防止双重电子邮件:
$('#myForm')
.on('valid.fndtn.abide', function (e) {
if(e.namespace != 'abide.fndtn') {
return;
}
var name = $("input#name").val();
var email = $("input#email").val();
var message = $("textarea#message").val();