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"
}
}
data['org'].file
anddata['pre'].file
are. What does your markup look like?