2012-12-18 54 views
1

我完全被難住了。我正在嘗試使用jQuery ajax函數將POST數據發送到電子郵件腳本。PHP郵件腳本在使用jQuery時沒有發送郵件.ajax()

這裏是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-\.][email protected]([\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成功函數但沒有發送電子郵件。

+0

需要看到另外 – hek2mgl

+1

你應該

元素的HTML表單真的使用'$(formelement).serialize()'來生成你的發佈數據。 – Musa

+0

另外你應該測試'$ mail-> Send();' – hek2mgl

回答

0

你的問題是數據:dataString。

要麼像序列如前所述,或嘗試這個東西similair:

數據:{basscent:basscent,bascolor:bascolor,}等