我正在測試使用jquery和ajax創建電子郵件表單。電子郵件處理併發送正常,但.ajax中的成功函數不會觸發。我對所有這些功能都很陌生,所以我不確定可能會出現什麼問題。我非常感謝你的幫助! FYI(#blah)僅僅是一個指標Jquery .ajax聯繫表單可以工作,但不會觸發成功
$(function() {
$(".button").click(function() {
// validate and process form here
$('#blah').hide();
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "http://www.joepolitic.com/contactform.php",
data: dataString,
success: function() {
$('#second').hide();
}
});
return false;
});
});
PHP:
<?php
// get posted data into local variables
$EmailFrom = "First Facility Contact Form";
$EmailTo = "[email protected]";
$Subject = "Contact";
$Name = Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Phone = Trim(stripslashes($_POST['phone']));
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
?>
使用Firefox + Firebug的。打開NET選項卡並觀看XHR請求。你可以看看它是否成功。 –
您可能會嘗試'echo $ success;'在文件末尾 – Joe