这是一个非常奇怪的问题。 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个小时试图解决这个问题,我认为自己非常绝望。任何帮助将不胜感激,请询问您是否需要更多信息。
答案 0 :(得分:2)
您必须设置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);
答案 1 :(得分:0)
尝试设置HTTP标头:
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");