这是一个很奇怪的问题。 Javascript IS 调用 PHP 脚本,而 PHP 脚本 IS 返回“something”(它返回 array ()
)。问题是,当我尝试通过 $_POST['ID']
获取发布数据的值时它基本上表示没有发布值,即使 sendData
确实包含 ID
的值。完整字符串sendData
(通过 alert(sendData)
获取)如下:
ID='1'&Invoice=''&FirstName=''&LastName=''&Description=''&Testimonial=''&ScreenRoom='false'&GlassWindow='true'
Javascript 文件: admin.js
function saveChanges() {
var sendData='ID=\'' + document.getElementById("ID").value + '\'';
sendData+='&Invoice=\'' + document.getElementById("Invoice").value + '\'';
sendData+='&FirstName=\'' + document.getElementById("FirstName").value + '\'';
sendData+='&LastName=\'' + document.getElementById("LastName").value + '\'';
sendData+='&Description=\'' + document.getElementById("Description").value + '\'';
sendData+='&Testimonial=\'' + document.getElementById("Testimonial").value + '\'';
sendData+='&ScreenRoom=\'' + document.getElementById("ScreenRoom").checked + '\'';
sendData+='&GlassWindow=\'' + document.getElementById("GlassWindow").checked + '\'';
var req = new XMLHttpRequest();
req.open("POST","scripts/saveChanges.php",true); //true indicates ASYNCHRONOUS
req.send(sendData);
req.onreadystatechange = function() {
//Is request finished? Does the requested page exist?
if(req.readyState==4 && req.status==200) {
//Your HTML arrives here
alert(sendData);
alert(req.responseText);
}
}
}
PHP 文件(正在发布到): saveChanges.php(位于/scripts/)
<?php
session_start();
if (!isset($_SESSION['group']) || $_SESSION['group'] != 'admin') {
die ('You do not have permission to access this page!');
}
print_r($_POST);
$ID=$_POST['ID'];
$Invoice=$_POST['Invoice'];
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$Description=$_POST['Description'];
$Testimonial=$_POST['Testimonial'];
$Date=$_POST['Date'];
$GlassWindow=$_POST['GlassWindow'];
$ScreenRoom=$_POST['ScreenRoom'];
?>
我通常只是在绝望的情况下才来这里,而且我现在已经花了大约 3 个小时来试图解决这个问题,我认为自己相当绝望。任何帮助将不胜感激,请询问您是否需要更多信息。
最佳答案
您必须设置 php 的内容类型才能读取变量
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
从发布的数据中删除引号并对其进行正确编码
var sendData='ID=' + encodeURIComponent(document.getElementById("ID").value);
sendData+='&Invoice=' + encodeURIComponent(document.getElementById("Invoice").value);
sendData+='&FirstName=' + encodeURIComponent(document.getElementById("FirstName").value);
sendData+='&LastName=' + encodeURIComponent(document.getElementById("LastName").value);
sendData+='&Description=' + encodeURIComponent(document.getElementById("Description").value);
sendData+='&Testimonial=' + encodeURIComponent(document.getElementById("Testimonial").value);
sendData+='&ScreenRoom=' + document.getElementById("ScreenRoom").checked;
sendData+='&GlassWindow=' + document.getElementById("GlassWindow").checked;
在发送请求之前设置就绪状态监听器
req.onreadystatechange = function() {
//Is request finished? Does the requested page exist?
if(req.readyState==4 && req.status==200) {
//Your HTML arrives here
alert(sendData);
alert(req.responseText);
}
}
req.send(sendData);
关于javascript - 为什么我的 Javascript Ajax 调用没有将值发送到我的 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640439/