我在尝试将联系表单通过电子邮件发送给我所有输入的表单值时遇到问题。目前我收到的电子邮件只显示“消息”值,而不是姓名,电话或电子邮件。我看了几个教程,但是我需要一些不重新加载页面的东西,所以我给ajax一个去。我对php或javascript一点都不强,所以我可能会错过很明显的东西......
脚本中注释掉的警报确实有效。
这是我的剧本:
$('#myform').on('valid.fndtn.abide', function() {
var Name = $("input#Name").val();
var Email = $("input#Email").val();
var Phone = $("input#Phone").val();
var Message = $("textarea#Message").val();
var dataString = 'name='+ Name + '&email=' + Email + '&phone=' + Phone + '&Message=' + Message;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "contactengine.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='success-message'></div>");
$('#success-message').html("<h2>Message sent!</h2>")
.append("<p>We will be in touch soon. If you haven't had a reply within 2 working days please check your junk folder and mark us as safe.</p>")
.hide()
.fadeIn(1500, function() {
$('#success-message');
});
}
});
return false;
});
我的php:
<?php
$EmailFrom = "me@myemail.co.nz";
$EmailTo = "me@myemail.co.nz";
$Subject = "New message from website contact form";
$Name = Trim(stripslashes($_POST['Name']));
$Phone = Trim(stripslashes($_POST['Phone']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= "\n";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>