javascript - Ajax 没有收到来自我的“工作 PHP 脚本”的响应
问题描述
我目前正在使用 Ajax、PHP 和 MySQL 的混合物制作联系页面。 一些快速说明:
所有的 PHP 和 AJAX 代码都在工作。我唯一无法工作的是从 PHP 到 AJAX 的响应。
此联系页面已经过优化,以便在浏览器上禁用 JavaScript 的情况下。该页面将使用一个简单的 POST 方法来完成该操作。
我有一些自定义函数(例如 n()),它们已在我提供剪辑的文件中较早声明。请忽略这些,因为它们不会影响与问题相关的任何事情。
每个文件的分解可以在每个代码块下面找到。
contact_page.php(魔法发生的地方)
header('Content-type: application/json');
if(isset($_POST['l'])) {
if($_POST['l'] == 'php') { //This is related to the PHP version of the form validation. The code that handles the AJAX is farther down
$fd = $_POST['fd'];
$i = 0;
while ($i < count($fd)) { //Used to make sure all fields have been filled. Otherwise, return null.
$fd[$i] = n($fd[$i],false);
if(empty($fd[$i])) { $fd[$i] = null; }
$i++;
}
if(isset($fd[0],$fd[1],$fd[2],$fd[3])) {
if(filter_var($fd[1], FILTER_VALIDATE_EMAIL) && strlen($fd[1]) > 6) {
$q1 = "INSERT INTO contact_msg VALUES ('".$fd[0]."','".$fd[1]."','".$fd[2]."','".$fd[3]."',NOW(),'$date-$id');";
$r1 = mysqli_query($dbc1,$q1);
if($r1) {
$h = 'From: Jewl Photography Notifications <contact@jewl-photography.net>' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'Reply-To: contact@jewl-photography.net' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$m = m($fd[0],$fd[2],$fd[3],"$date-$id");
mail('nathan@lmartin.net','New Message From: '.$fd[0],$m,$h);
header('Location: https://jewl-photography.net/Contact?w=SNT');
} else {
header('Location: https://jewl-photography.net/Contact?w=INT_ERR');
}
} else {
header('Location: https://jewl-photography.net/Contact?w=FLS_EMAIL');
}
} else {
header('Location: https://jewl-photography.net/Contact?w=MISS_ALL');
}
}
//Below is the code that handles the AJAX
if($_POST['l'] == 'ajax') {
if(isset($_POST['name'],$_POST['email'],$_POST['subject'],$_POST['message'])) {
$fd = array(n($_POST['name'],true),
n($_POST['email'],false),
n($_POST['subject'],false),
n($_POST['message'],false));
if(filter_var($fd[1], FILTER_VALIDATE_EMAIL)) {
$q1 = "INSERT INTO example_table VALUES ('".$fd[0]."','".$fd[1]."','".$fd[2]."','".$fd[3]."',NOW(),'$date-$id');";
$r1 = mysqli_query($dbc1,$q1);
if($r1) {
echo json_encode('SENT');
$h = '
**Header Info**
';
$m = m($fd[0],$fd[2],$fd[3],"$date-$id");
mail('example@example.net','New Message From: '.$fd[0],$m,$h);
} else {
echo json_encode('ERROR_ADD');
}
} else { echo json_encode('FALSE_EMAIL'); }
} else { echo json_encode('NOT_ALL'); }
}
}
contact_page.php 非常简单。
它从 Ajax 获取 POST 信息(如下所示)。
通过许多编码函数(例如 htmlspecialchars() 等,由 n() 表示)运行它。
还测试是否满足某些要求。如果不是,它应该将响应发送回 AJAX(参见第 6 步)
将其添加到 SQL 表中。
向版主发送电子邮件,让他们知道已发送新消息。
然后使用 json 对象将响应发送回联系人页面: echo json_encode('Response Text Here');
除第 6 步外,一切正常。出于某种原因,AJAX 拒绝接收响应。我没有收到任何 PHP、SQL 或 JavaScript 错误或警告,而且我的 Ajax(如下所示)正在使用 dataType JSON。
Contact.php(用户端联系页面)
<script>
//This first half isn't ajax, skip down a few lines
//The very bottom of tis block of code is the form's html
$(function() {
$('#fglk').submit(function() {
var e = [];
e[0] = $('#name').val();
e[1] = $('#emal').val();
e[2] = $('#subj').val();
e[3] = $('#mesg').val();
if(e[1].length > 6 && e[1].length < 255) {
$('.err-span').removeClass('error');
$('.err-span').html('');
} else {
$('.err-span').addClass('error');
$('.err-span').html('Provide valid Email!');
e[1] = null;
}
/**AJAX Related code \/ **/
if(e[0] && e[1] && e[2] && e[3]) {
var data = new Object();
data.l = 'ajax';
data.name = e[0];
data.email = e[1];
data.subject = e[2];
data.message = e[3];
var options = new Object();
options.data = data;
options.dataType = 'json';
options.type = 'post';
options.success = function (response) {
if(response == 'SENT') {
$('.err-span').html('Sent!');
$('.err-span').addClass('sent');
$('.err-span').addClass('error');
} else if(response == 'ERROR_ADD') {
$('.err-span').html('An internal error prevented your message from being sent!');
$('.err-span').addClass('error');
} else if(response == 'NOT_ALL') {
$('.err-span').html('Please fill out all fields!');
$('.err-span').addClass('error');
} else if(response == 'FALSE_EMAIL') {
$('.err-span').html('You must provide a valid email!');
$('.err-span').addClass('error');
}
};
options.url = 'https://example.net/php/contact_page.php';
$.ajax(options);
} else {
}
return false;
});
});
</script>
<p style='color: red;'>
<? //These $_GET parameters are for if raw PHP is used to send the message
if($_GET['w'] == 'INT_ERR') {
echo '**Some Text**';
}
if($_GET['w'] == 'FLS_EMAIL') {
echo '**Some Text**';
}
if($_GET['w'] == 'MISS_ALL') {
echo '**Some Text**';
}
if($_GET['w'] == 'SNT') {
echo '**Some Text**';
}
?>
</p>
<form action='https://example.net/php/contact_page.php?l=php' id='fglk' method='post'>
<label>Full Name:</label><br><input type='text' id='name' name='fd[]' required><br>
<label>Email:</label><br><input type='email' id='emal' name='fd[]' required><br>
<label>Subject:</label><br><input type='text' id='subj' name='fd[]'><br>
<label>Message:</label><br><textarea id='mesg' name='fd[]' required></textarea><br>
<span class='err-span'></span>
<input type='submit' name='fd[]' value='Send'>
</form>
Contact.php 是非常不言自明的。
它从表格中获取信息。
通过基本电子邮件验证运行电子邮件
将其传递给一些 JSON 对象。
通过 $.ajax() 函数运行这些对象。
发出返回 false;以防止表单发送和重新加载页面。
PHP 运行后,AJAX 应获取响应并发送相应写入的消息。
它不会在我的控制台中引发任何错误。如果我提供的电子邮件长度小于 6 个字符,它将显示错误(尽管这与 PHP 代码无关)。但不会显示任何响应。
我之前使用过相同的 AJAX 代码,它运行良好,主要区别在于 PHP 后端。
如果您对代码有任何疑问,请告诉我!
谢谢你的帮助!
解决方案
推荐阅读
- linux - 如何在我的 Linux 系统上安全地重新配置 gcc?
- python - 为什么 X=1 概率质量函数和累积密度函数不同?
- javascript - 如何绘制垂直面积图?
- python - CSV 文件 - 无法从我的 csv 打印特定值/实体
- domain-driven-design - DDD,抽象类,聚合和存储库,一个或多个存储库?
- javascript - ESLint:分配给函数参数的属性
- python - 201901 - relativedelta(weeks=1) 得到 201900
- reactjs - JSX/React 中的自定义命名空间属性
- php - Symfony 4 - 使用 ROLE_FITTER 类型的用户填充数组
- javascript - JS通过三种排序方式对数组进行排序