首先;对不起,如果之前有回复,我已经搜索了最近几天没有运气。
我正在使用PHPMailer 5.2.0从表单发送邮件,该表单由来自带有jQuery form的jQuery脚本的AJAX调用调用,每次我发送它发送两次的邮件。必须是脚本被调用两次,但我无法弄清楚它发生的位置。
这是php脚本:
<?php
require_once('PHPMailer_5.2.0/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$navn= $_REQUEST['navn'];
$tlf= $_REQUEST['tlf'];
$ring= isset($_REQUEST['ring']) ? $_REQUEST['ring'] : 0;
if($ring == 0)
{
$ring2 = " - og vil gerne ringes tilbage.";
}
else
{
$ring = "";
}
$email= $_REQUEST['email'];
$sendToOwn= isset($_REQUEST['sendToOwn']) ? $_REQUEST['sendToOwn'] : 0;
$besked= $_REQUEST['besked'];
$mail = new PHPMailer();
//$body = "testing 30/11";//file_get_contents('contents.html');
//$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "SMTP.Server.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SetFrom('noreply@sentfromDomain.dk', 'Name');
$mail->ClearReplyTos("");
$mail->AddReplyTo("mail@adress.com","Name");
$mail->Subject = "Besked fra mjtransportogflyt.dk";
$mail->MsgHTML("TEST FOR HELVEDE");//"<p>Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."</p><p>".$besked."</p>");
$mail->AltBody = "Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."<br /> ".$besked; // optional, comment out and test
//$mail->MsgHTML($body);
$address = "Sendto@thisadress.com";
$mail->AddAddress($address, "Kasper John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
这是jQuery:
$(document).ready(function(){
function validate(formData, jqForm, options) {
for (var i=0; i < formData.length; i++) {
if (!formData[i].value) {
$('#results').removeClass('info').addClass('error');
$('#results').html('<p><strong>Udfyld venligst alle felter</strong></p><p>Din besked blev <strong>ikke</strong> sendt</p>');
//Test om det bliver kaldt
//alert('works?');
return false;
}
}
var options = {
//target: '#results',
success: showResponse
};
function showResponse(statusText){
$('#results').removeClass('info').addClass('success');
$('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
//Får tilbagemelding fra processed.php
//alert(statusText);
}
$('#myform').ajaxSubmit(options);
}
$('#myform').ajaxForm({beforeSubmit:validate})
});
最后一个形式是:
<form id="myform" method="post" action="processed.php">
Lots and lots of inputs and stuff
<!--submit button below-->
<button class="btn action">Send besked</button>
</form>
答案 0 :(得分:1)
我认为您的问题是您将ajaxForm
添加到表单中,该表单本身会自动发送表单。但是,在发送之前,您将其发送到验证它的validate
函数,然后您也使用ajaxSubmit
在那里提交表单。
删除ajaxSubmit
并修复它,因为ajaxForm
会自行提交
我认为这会起作用
$(document).ready(function() {
var options = {
beforeSubmit: validate, // pre-submit callback
success: showResponse // post-submit callback
};
// bind form using 'ajaxForm'
$('#myForm').ajaxForm(options);
});
function showResponse(responseText, statusText, xhr, $form){
$('#results').removeClass('info').addClass('success');
$('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
//Får tilbagemelding fra processed.php
//alert(statusText);
}
function validate(formData, jqForm, options) {
for (var i=0; i < formData.length; i++) {
if (!formData[i].value) {
$('#results').removeClass('info').addClass('error');
$('#results').html('<p><strong>Udfyld venligst alle felter</strong></p><p>Din besked blev <strong>ikke</strong> sendt</p>');
//Test om det bliver kaldt
//alert('works?');
return false;
}
}
}