簡體   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