繁体   English   中英

为什么我的Javascript Ajax不会将值发布到我的PHP脚本?

[英]Why isn't my Javascript Ajax call posting values to my PHP script?

这是一个非常奇怪的问题。 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);

尝试设置HTTP标头:

req.setRequestHeader("Content-type","application/x-www-form-urlencoded");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM