我正在尝试通过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);
?>
答案 0 :(得分:0)
尝试使用此语法将变量作为对象发布
$.ajax ({
type: "POST",
url: "process.php",
dataType: 'json'
data: {
dataString : name,
email : email,
subject :subject,
msg : msg
},
success: function() {
$("#success").show();
$( '#contactUs' ).each(function(){
this.reset();
});
}
});