我正在处理我希望在不刷新页面的情况下发送的 wordpress 网站联系表。我设法让它工作,它发送邮件,但后来我想如果它不成功怎么办,并添加了错误功能。现在,它每次都调用错误函数,即使它设法发送邮件。这是它的代码:
$("#Submit").click(function () {
var dataString = $('#knamecontactform').serialize();
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "http://example.com/form.php",
data: dataString,
dataType: "text",
error: function () {
$('#messagenotsent').fadeIn(400);
},
success: function () {
$('#knamecontactform')[0].reset();
$('#messagesent').fadeIn(400);
}
});
return false;
});
和 submitform.php:
<?php
$to = "mail@gmail.com";
$subject = "Message";
$message =
$_POST['Nimi'] . "\n" .
$_POST['Puhelin'] . "\n" .
$_POST['Sposti'] . "\n" .
$_POST['Tiedot'];
$from = $_POST['Nimi'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo 'Mail sent.';
?>