2014-01-17 137 views
-2

我試圖通過ajax將聯繫表單提交給php文件。我使用phpMailer發送表單結果。然而,POST似乎沒有工作。提交時,成功或錯誤條件都不會執行。當我註釋掉POST變量時,ajax請求成功執行並且電子郵件發送(變量爲空)。下面是代碼...

<form method="post" class="form2" id="form2Id" name="form" action=""> 

      <div class="formtitle">Form<</div> 

      <div class="input"> 
       <div class="inputtext">Your Name: </div> 
       <div class="inputcontent"> 

        <input id="nameId" type="text" name="name" /> 

       </div> 
      </div> 

      <div class="input"> 
       <div class="inputtext">Your Phone Number: </div> 
       <div class="inputcontent"> 

        <input id="phoneNumberId" type="text" name="phoneNumber" /> 

       </div> 
      </div> 

      <div class="input"> 
       <div class="inputtext">Your Email Address: </div> 
       <div class="inputcontent"> 

        <input id="emailAddressId" type="text" name="emailAddress"/> 

       </div> 
      </div> 

      <div class="inputtextbox nobottomborder"> 
       <!--<div class="inputtext">Message: </div>--> 
       <div class="inputcontent"> 

        <!--<textarea id="messageId" class="textarea" name="message"></textarea>--> 

       </div> 
      </div> 

      <div class="buttons"> 

       <input class="orangebutton" type="submit" id="formSubmit" value="Submit" /> 

      </div> 

     </form> 

$("#formSubmit").click(function() { 
$("#form2Id").validate({ 
debug: true, 
submitHandler: function(form) { 

$.ajax({ 
type: "POST", 
url: "handler.php", 
data: $(form).serialize(), 
timeout: 12000, 
success: function() { alert("Success"); }, 
    error: function() { alert("FAILURE"); } 
}); 
return false; 
} 
}); 
}); 

)}; 

<?php 

require("./PHPMailer-master/class.phpmailer.php"); 
include("./PHPMailer-master/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

//collect the posted variables into local variables before calling $mail = new mailer 

$nameId = $_POST['nameId']; 
$phoneNumberId = $_POST['phoneNumberId']; 
$emailAddressId = $_POST['emailAddressId']; 

$mail = new PHPMailer(); 

//$body = file_get_contents('contents.html'); 
//$body = eregi_replace("[]",'',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host = "lll"; // SMTP server 
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing) 
// 1 = errors and messages 
// 2 = messages only 
$mail->SMTPAuth = true; // enable SMTP authentication 
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier 
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server 
$mail->Port = 465; // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "..."; // GMAIL password 

$mail->SetFrom('[email protected]', 'UUU'); 

$mail->AddReplyTo("[email protected]","UUU"); 

$mail->Subject = "Email subject"; 
optional, comment out and test 
$mail->MsgHTML($body); 


$address = $emailAddressId; 
$mail->AddAddress($address, "MMM"); 



$mail->Body=" 
Name: $nameId 
Phone: $phoneNumberId 
Email: $emailAddressId 
"; 

if(!$mail->Send()) { 
echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
echo " 
<h2>Thank you. We will contact you shortly.</h2> 
"; 
} 
?> 

任何幫助將不勝感激。

+0

嘗試print_r($ _ POST);在PHP中,看看有什麼變量得到postin到頁面,然後從它解析你的需要值,因爲在ajax數據選項中使用serilaze數據 –

+1

因此,在掛鉤到表單的過程中,通過ajax提交它,在PHP中處理它,發送回覆並通過電子郵件發送其內容,出現問題?請縮小你的問題。將其分解爲一小段代碼來重現問題。 – GolezTrol

+0

請嘗試縮小您的問題http://stackoverflow.com/help/mcve –

回答

2

鑑於html行<input id="nameId" type="text" name="name" />和此php代碼$nameId = $_POST['nameId'];你確實最終會得到一個空的$nameId,因爲發佈的值在$_POST['name']。其他變量也是如此。

0

如果我註釋掉POST變量,

$nameId = $_POST['name']; 
$phoneNumberId = $_POST['phoneNumber']; 
$emailAddressId = $_POST['emailAddress']; 

的電子郵件,只要我在一個有效的電子郵件地址輸入成功發送。否則,電子郵件根本不會發送。在這兩種情況下,ajax都會返回成功的響應。我已經嘗試了使用名稱而不是ID的上述建議,但它不適用。