我正在尝试通过 PHP 将表单作为电子邮件提交,但我通过 jQuery 中的 AJAX 函数调用发送数据,因此页面本身不会刷新(简而言之,用户填写表单,然后当他们点击提交,它将数据设置为一个变量,并通过 AJAX 调用将其发送到 PHP 页面,所有这些都无需重新加载页面)但由于某种原因,电子邮件没有发送,但代码本身似乎起作用了?
这是我拥有的代码(只是 AJAX 调用和 php 代码)。
var dataString = 'name=' + name + '&email=' + email + "&subject=" + subject + '&msg=' + msg;
$.ajax ({
type: "POST",
url: "process.php",
data: dataString,
success: function() {
$("#success").show();
$( '#contactUs' ).each(function(){
this.reset();
});
}
});
PHP
<?php
if(isset($_POST['email'])) {
$email_to = "xxx@someplace.net";
$email_subject = "subject";
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$message = $_POST['msg']; // required
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $message, $headers);
?>