- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个联系页面 HTML Form
.
我使用jQuery
验证字段。然后使用jQuery's .ajax()
方法将信息发送到 php 文件,该文件处理并通过 mail()
发送电子邮件方法。
PHP File
内我有一个IF
检查是否有 POST variable
的语句这样设置我就可以将整个网站上多个表单的所有表单处理放在一个页面上。
我获取所有变量并使用 PHP
进行另一个表单字段验证。然后我构建 HTML Email
它是标题。
如果 PHP 验证成功,我将通过 mail()
发送电子邮件方法。然后我检查 mail()
是否方法成功,如果是这样,我会发送另一封“自动回复”电子邮件。
IF
内部检查 mail()
是否存在的语句方法成功我echo
成功或错误消息 json_encode()
.
当用户单击表单上的提交按钮时,我将其设置为返回 false,以便它保留在同一页面上,并在发送成功时显示一条消息。
两封电子邮件均已从表单成功发送。除了我的.ajax()
方法没有收到来自 php 文件'json_encode()
的成功或错误消息.
我删除了 jQuery .click 中的 return false 并尝试了标准 PHP echo
在 php IF
里面检查帖子 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 代码中......我想。
最佳答案
好的,感谢上面 @PatrickEvans 和 @MartyMcKeever 的建议,我通过查看 Firebug 控制台发现我的一个自定义 PHP 函数缺少必需的参数来解决这个问题。这导致了解析错误,然后使我的 jQuery 抛出错误,但仍然允许 PHP 处理电子邮件。
如果您查看上面的 PHP 代码,会发现有一个方法表示 $nextEventDay = getDay();
应该是$nextEventDay = getDay("today");
。这导致了所有问题
给@MartyMcKeever 的评论投赞成票。
关于javascript - jQuery ajax 调用 php 处理表单页面未返回成功或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028013/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!