我完全被这件事难住了。我正在尝试使用 jQuery ajax 函数将 POST 数据发送到电子邮件脚本。
这是jQuery代码。
$('#bas-submit-button').click(function () {
var baslogo = $('input#bas-uploaded-logo').val();
var basscent = $('input[name="scent"]:checked').val();
var bascolor = $('input[name="color"]:checked').val();
var basdistributor = $('select#bas-distributor-name').val();
var basname = $('input#bas-contact-name').val();
var basemail = $('input#bas-contact-email').val();
var bascontactphone = $('input#bas-contact-phone').val();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var baspantone = $('input#pantone').val();
var basclip = $('input[name="clip-name"]:checked').val();
var dataString = 'bas-contact-name=' + basname + '&bas-contact-email=' + basemail + '&bas-contact-phone=' + bascontactphone + '&bas-uploaded-logo=' + baslogo + '&scent=' + basscent + '$color=' + bascolor + '&clip-name=' + basclip + '&pantone=' + baspantone + '&bas-distributor-name=' + basdistributor;
$.ajax({
type: "POST",
url: "build-a-smen-submit.php",
data: dataString,
success: function () {
$("#bas-submit-message").removeClass("error").addClass("success").html("Virtual Sample Submitted!").fadeIn(200).delay(5000).fadeOut(200);
}
});
return false;
});
还有我的 PHP 脚本。
<?
/* config start */
$emailAddress = '*****';
$ccAddress = '*****';
/* config end */
require "class.phpmailer.php";
session_start();
if($_POST['bas-submit-button']=='Submit'){
$msg='Name: '.$_POST['bas-contact-name'].'<br />
Email: '.$_POST['bas-contact-email'].'<br />
Phone: '.$_POST['bas-contact-phone'].'<br /><br />
Options<br /><br />
Logo File: '.$_POST['bas-uploaded-logo'].'<br />
Scent Selection: '.$_POST['scent'].'<br />
Accent Color: '.$_POST['color'].'<br />
Name on clip: '.$_POST['clip-name'].'<br />
Pantone Colors: '.$_POST['pantone'].'<br />
Distributor: '.$_POST['bas-distributor-name'].'<br /><br />
Submitting IP: '.$_SERVER['REMOTE_ADDR'];
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['bas-contact-email'], $_POST['bas-contact-name']);
$mail->AddAddress($emailAddress);
$mail->AddCC($ccAddress);
$mail->SetFrom($_POST['bas-contact-email'], $_POST['bas-contact-name']);
$mail->Subject = 'Build-A-Smen Promotional Product Interest';
$mail->MsgHTML($msg);
$mail->Send();
if($_POST['ajax']){
echo '1';
}
else{
if($_SERVER['HTTP_REFERER'])
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
}
};
?>
我相信问题出在从 jQuery 发送 POST 数据的某个地方。如果我注释掉我的 jQuery 代码,则 php 脚本可以正常工作,但是当我使用 jQuery ajax 函数运行它时,我会触发 ajax 成功函数但没有电子邮件。