我目前在此站点上有一个联系表,我希望将其从 php 结果迁移到 ajax 结果。我使用了带有此代码的 mail.php 文件:
<?php
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$email = $_POST['email'];
$tel1 = $_POST['tel1'];
$tel2 = $_POST['tel2'];
$tel3 = $_POST['tel3'];
$reason = $_POST['reason'];
$message = $_POST['text'];
$formcontent="From: $firstname $lastname \n Email address: $email \n Telephone Number: $tel1 $tel2 $tel3 \n Reason: $reason \n Message: $message";
$recipient = "ilanbiala@ilanshomekitchen.x10.mx";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<script>alert('Thank you!');</script>";
?>
这个 php 文件发送了一封包含联系表单结果的电子邮件。但是,它还重新加载了页面并在空白页面上显示了一个警告框,这不是我想要的。我正在寻找使用 jquery ajax 函数来制作它,以便 ajax 将信息发送到我的 php 文件以发送邮件,除非我可以直接从 ajax 邮寄联系表格以获得结果。
联系表单是通过这个 ajax 函数提交的:
function submitForm(form) {
var form = $("form").submit(function() {
var formData = new FormData(form);
console.log("formData");
var XHR;
if (window.XHMLHttpRequest){XHR = new XHMLHttpRequest();}
else {XHR = new ActiveXObject("Microsoft.XMLHTTP");}
XHR.open("POST","mail.php",true);
console.log("XHR opened.");
XHR.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
XHR.send(formData);
console.log("Sent formData.");
}
}
});
}