0

I have my javascript here on checkTests.php, which is a function as follows

        function confirmedUpload(){
            //Make the button so it can't be clicked again
            document.getElementById('confirmUpload').setAttribute("disabled", true);

            //This is the data we are posting
            var postData = JSON.stringify(<?php echo $uploadJSON; ?>);

            //This bit works OK and I see the string of JSON printed in the console.
            console.log(postData);

            //Create an XMLHttpRequestObject
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "/uploadTests.php", true);

            xhr.setRequestHeader( "Content-Type", "application/json" );
            xhr.send(postData);

            //Wait for the page to respond.
            xhr.onreadystatechange = function()
            {
                if(xhr.readyState == 4 && xhr.status == 200)
                {
                    console.log(this.responseText);
                }
            }
        }

which I would expect to post the data postData to the page uploadTests.php and then return the array(data) from the uploadTests.php page in the console.

But what actually happens is that the post request is made, I see the output console.log(postData); from the javascript, but then when uploadTests.php returns the value, all I get is array() 0 and there's no data.

contents of uploadTests.php is:

<?php

    print_r($_POST);
    echo '   ' . sizeof($_POST);

?>

why does uploadTests.php think that the $_POST was blank?

Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22

1 Answers1

1

I believe that you need to set your Content Type to application/x-www-form-urlencoded for PHP to read the $_POST superglobal array. Otherwise the content appears in the PHP standard input body which is retrievable via php://input. See here for more details.

EDIT:

If you can't change the content type, you'll need to read the data from the php://input stream which will retrieve the request's content body.

War10ck
  • 12,387
  • 7
  • 41
  • 54