0

I have the following JS:

window.onload = function() {
'use strict';
var ajax = getXMLHttpRequestObject();

ajax.onreadystatechange = function() {
    if ( ajax.readyState == 4 ) {
        if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) {
            var data = JSON.parse(ajax.responseText);
            var file = '';
            file += 'Original: ' + data['org'].file + '<br>';
            file += 'Processed: ' + data['pre'].file + '<br>';
            document.getElementById('output').innerHTML = file; 
        } else {
            document.getElementById('output').innerHTML = 'Error: ' + ajax.statusText;
        }
    }
};

document.getElementById('btn').onclick = function() {
    ajax.open('POST', 'resources/test.json', true);
    ajax.setRequestHeader('Content-Type', 'application/json');
    ajax.send(null);
};
};

I would like to pass the data from

data['org'].file

and

data['pre'].file

to PHP and have it echo out the value using the POST method. Please no jQuery solutions this needs to be strictly JavaScript.

Something like this:

<?php $data = $_POST['the_data']; echo $data; ?>

Here is the JSON from test.json:

{
"org": {
    "file": "css/original.css"
},
"pre": {
    "file": "css/preprocessed.css"
}
}
2
  • I'm very confused about what data['org'].file and data['pre'].file are. What does your markup look like?
    – 000
    Commented Apr 5, 2013 at 18:08
  • data['org'].file and data['pre'].file is the JSON, I'll post the JSON code now.
    – thinkus
    Commented Apr 5, 2013 at 18:10

2 Answers 2

0

If you want a PHP script to echo JSON data, it's as simple as this:

<?
    $json = file_get_contents('php://input');
    //...
    echo $json;
?>

To post it from Javascript, I reccomend reading this. There's a reason so many people use jQuery...

3
  • I've tried that method but had no success. Not sure the data is available and hence nothing is echoed. I'm looking into the link you sent but it seems to be consistent around information gathered from a form and not JSON. If the information is available through the DOM I would not be in this pickle. Thanks for the link.
    – thinkus
    Commented Apr 5, 2013 at 18:19
  • I've been using that exact code for over 8 months (and several million JSON requests) without issue. What exactly goes wrong? That code should take any JSON data POSTed to the page and return it exactly.
    – GJK
    Commented Apr 5, 2013 at 18:25
  • My impression is that the JSON data isn't loaded initially so there is nothing to echo. The user interacts with a button but by the time the data is parsed I believe the echo statement has already run. The variable $json is undefined.
    – thinkus
    Commented Apr 5, 2013 at 18:42
0

have a look on JSON Stringify you can use that to encode your data.

Then after you encoded you can send it wherever you need.

5
  • That's not necessarily true. There are a LOT of characters valid in JSON that aren't valid in certain places, such as HTTP cookies or URL addresses. If you use JSON.stringify(), you more likely than not need to encode it specially as well.
    – GJK
    Commented Apr 5, 2013 at 18:18
  • I've tried using stringify but passing the data to PHP has proven to be more a difficult process. Once I use stringify what's my next move?
    – thinkus
    Commented Apr 5, 2013 at 18:23
  • after you encode it you basically have some "text" that you can send using ajax openjs.com/articles/ajax_xmlhttp_using_post.php when you get it on the server side you can decoded with json_decode() Commented Apr 5, 2013 at 18:42
  • I'll have a look at the article and get back to you. Thank you.
    – thinkus
    Commented Apr 5, 2013 at 18:47
  • You're welcome don't forget to choose it as a good answer if it helped Commented Apr 5, 2013 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.