【发布时间】:2014-09-24 23:13:41
【问题描述】:
我有一个带有HTML Form 的联系页面。
我使用jQuery 来验证字段。然后使用jQuery's .ajax()方法将信息发送到php文件中,通过mail()方法发送邮件。
在PHP File 中,我有一个IF 语句,用于检查POST variable 是否设置为这样,我可以将所有表单处理放在一个页面上,用于整个网站的多个表单。
我得到所有变量并使用PHP 进行另一个表单字段验证。然后我构建了HTML Email 和它的标题。
如果 PHP 验证成功,我会通过 mail() 方法发送电子邮件。然后我检查mail() 方法是否成功,如果成功,我发送另一封“自动回复”电子邮件。
在检查mail() 方法是否成功的IF 语句内部,echo 使用json_encode() 显示成功或错误消息。
当用户单击表单上的提交按钮时,我将其设置为返回 false,以便它停留在同一页面上并在发送成功时显示一条消息。
两封电子邮件都从表单成功发送。除了我的.ajax() 方法没有收到来自 php 文件的json_encode() 的成功或错误消息。
我删除了 jQuery .click 中的 return false 并在 php IF 语句中尝试了一个标准的 PHP echo 语句,该语句检查是否发布 isset() 并且我无法将其打印到浏览器。 json_encode 也不会打印到浏览器。但是,当我将PHP echo 放在IF 语句之外时,它打印得很好。这让我感到困惑,因为它显然在 if 语句中发送电子邮件但不会回显。我做错了什么?
jQuery
$("#contactFormSubmit").click(function(){
// validate and process form here
// NAME VALIDATION
var name = $("input#nameField").val();
if (name == "") {
$("input#nameField").focus();
$("input#nameField").css("border","1px solid red");
alert("nameFieldError");
return false;
}
// EMAIL VALIDATION
var formEmail = $("input#emailField").val();
if (formEmail == "" || !validateEmail(formEmail)) {
$("input#emailField").focus();
$("input#emailField").css("border","1px solid red");
alert("emailFieldError");
return false;
}
// PHONE VALIDATION
var phone = $("input#phoneField").val();
var phoneReg = "/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/";
if (phone == "" || !isValidUSPhoneFormat(phone)) {
//alert("phone is wrong");
$("input#phoneField").focus();
$("input#phoneField").css("border","1px solid red");
alert("phoneFieldError");
return false;
}
var message = $("textarea#messageField").val();
var subject = $("#subjectField").val();
var dataString = 'name='+ name + '&email=' + formEmail + '&phone=' + phone + '&subject=' + subject + '&message=' + message + '&submitContactForm=1';
$.ajax({
type: "POST",
url: "process-form.php",
data: dataString,
dataType:"json",
success: function(response) {
if(response.status === "success") {
alert("success");
// do something with response.status or other data on success
} else if(response.status === "error") {
alert("error");
// do something with response.status or other data on error
}
},
error: function(xhr,errmsg) { alert(errmsg); }
});
return false;
});
PHP 文件
<?php
include "includes/functions.php";
if(isset($_POST['submitContactForm'])) {
$nextEventDay = getDay();
$eventToShow = "";
$dayToShow = "";
$dateToShow = "";
if ($nextEventDay == "Sunday" || $nextEventDay == "Monday" || $nextEventDay == "Tuesday" || $nextEventDay == "Wednesday" ||$nextEventDay == "Thursday" || $nextEventDay != "Friday" || $nextEventDay != "Saturday") {
$eventToShow = "thurEvent";
$dayToShow = "THU";
$dateToShow = getNextThursdayDate();
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$messageBody = $_POST['message'];
$sendTo = "xxxx@xxxx.com";
$confirmTo = $email;
//HTML EMAIL
$headers = "From: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//COMPANY EMAIL
$message5 = '<html><style type="text/css">table, td, tr, th{border:none !important;border-color:#111 !important;border-collapse:collapse;}a{color:#c92626 !important;}.title{color:#aaa;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
$message5 .= '<table rules="all" style="background-color:#111;border:none !important;width:100%;" cellpadding="10" border="0">';
$message5 .= '<tr style="border:none;"><td style="border:none;padding:20px 0px !important;"><img src="images/logo.jpg" alt="Nightclub" style="display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;" width="260" /></td></tr>';
$message5 .= "<tr style='border:none;'><td style='border:none;'><img src='/images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name:</strong> " . strip_tags($name) . "</td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;' class='title' ><strong style='color:#aaa;'>Email:</strong> <span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> <span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> " . strip_tags($subject) . "</td></tr>";
$message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> " . strip_tags($messageBody) . "</td></tr>";
$message5 .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
$message5 .= "</table>";
$message5 .= "</body></html>";
//CLIENT EMAIL
$areply = '<html><style type="text/css">table, td, tr, th {border:none !important;border-color:#111 !important;border-collapse:collapse;}a {color:#c92626 !important;}.title{color:#aaa;}#date a{color:#fff !important;text-decoration:none;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
$areply .= "<table rules='all' style='background-color:#111;border:none !important;width:100%;' cellpadding='10' border='0'>";
$areply .= "<tr style='border:none;'><td style='border:none;padding:20px 0px !important;'><img src='images/logo.jpg' alt='Nightclub Ann Arbor' style='display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;' width='260' /></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;'><img src='images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
$areply .= "<tr style='border:none; background:#151515;'><td style='border:none;text-align:justify;background-color:#161616;'><div style='float:left;display:inline-block;background-color:#000;margin:0px 10px 10px 0px;font-size:197%; padding: 25px 30px;'><p id='date' style='margin:0;color:#fff !important;'> \r\n <strong>" . $dateToShow ."</strong></p><p style='margin:0;color:#c92626 !important;'>" . $dayToShow ."</p></div><p style='margin-top:10px;margin-right:15px;'>Thank you for contacting us at Nightclub . We look forward to assisting you. Below is the information that we recieved and we will be contacting you as soon as possible. Thank you again, and we look forward to speaking with you. If you have any additional questions please contact us at our website (<a href='' style='color:#c92626'></a>), give us a call <span style='color:#c92626 !important;'></span>, or send us an Email <span style='color:#c92626 !important;'></span></p></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name: </strong>" . strip_tags($name) . "</td></tr>";
$areply .= "<tr style='border:none; background-color:#161616 !important;' ><td style='border:none !important;background-color:#161616 !important;' class='title'><strong style='color:#aaa;'>Email:</strong> <span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> <span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
$areply .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> " . strip_tags($subject) . "</td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> " . strip_tags($messageBody) . "</td></tr>";
$areply .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
$areply .= "</table>";
$areply .= "</body></html>";
$subject2 = "Thank you for your expressed interest ()";
$noreply = "xxx@xxx.com";
$headers2 = "From: " . $noreply . "\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
echo json_encode(array(
'status' => 'error'
//'message'=> 'error message'
));
} else {
$send = mail($sendTo, $subject, $message5, $headers);
}
if($send){
echo json_encode(array(
'status' => 'success'
//'message'=> 'success message'
));
$send2 = mail($email, $subject2, $areply, $headers2);
} else {
echo json_encode(array(
'status' => 'error'
//'message'=> 'success message'
));
}
}
?>
更新
我将错误调用添加回jQuery ajax() 方法并发现我收到了错误我还从我的PHP文件中删除了所有内容,除了:
echo json_encode(array(
'status' => 'success'
//'message'=> 'success message'
));
我仍然从jQuery ajax() 方法中收到错误消息。所以它必须在我的 jQuery 代码中......我想。
【问题讨论】:
-
我会推荐 PHP heredoc 语法来编写这样的长 HTML 字符串。
-
我在您的代码中的任何地方都没有看到 ajax 调用...
-
在您的
success回调旁边添加一个error回调,以查看请求是否出错。还要检查您的 php 服务器日志以检查报告的错误 -
@PatrickEvans 你说的是 jQuery 或 PHP json_encode 中的错误回调吗?
-
检查你的萤火虫控制台。您应该会在此处看到请求和响应。
标签: javascript php jquery ajax