我在一个网页上有两个电子邮件表单。表单仅在提交一次时正确发送电子邮件,但是当我第二次或更多时间点击提交按钮时,不仅会发送包含数据的电子邮件,还会发送其他空白电子邮件。我无法弄清楚如何解决它。这是代码:
HTML:
<form id="notify-form" name="notify-form" action="#" method = "post">
<div class="input-group">
<input class="input-field" type="email" id="email-notify" name="emailnotify" placeholder="Type your email..." required>
<span class="input-label">
<button class="btn-submit" onclick="ajax_news();"><i class="icon-arrow"></i></button>
</span>
</div>
</form>
脚本:
function ajax_news(){
var form = $('#notify-form'); //form id here
form.submit(function(event){
event.preventDefault();
var form_newsstatus = $('<div class="form_status"></div>');
var $form = $(this);
$.ajax({
type: 'POST',
url: "process-news.php",
data:{
Email2:$form.find("input[name='emailnotify']").val()
},
beforeSend: function(){
form.append(form_newsstatus.html('Sending...').fadeIn());
}
}).done(function(data){
form_newsstatus.html('Thank you! We will notify you soon!').delay(5210).fadeOut();
});
//delete messages when submit
document.getElementById("email-notify").value= "";
});
}
function ajax_post(){
//Similar code for the second form
}
PHP:
<?php
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
//variables
$email_address = strip_tags(htmlspecialchars($_POST['Email2']));
// Create the email and send the message
$to = 'email@email.com';
$email_subject = "Email subject";
$email_body = "Email message:\n\nEmail: $email_address\n\n\n";
$headers = "From: email@email.com\n";
$headers .= "Reply-To: $email_address";
if(mail($to,$email_subject,$email_body,$headers)){ echo "Mail sent!";}
else{ echo "Error, check your logs."; }
return true;