I make a system for calculating taxes on roads in the Netherlands, where I received several JS arrays (where the data is), which I parse with JS (all at the same time, because it is the same data format), and then pass it to PHP as a JSON format using an XMLHttpRequest object.
To do this, I first made this data mapping file:
var roadTaxData = { provinceWeightFuelPricesData: { personen_auto: { noord_holland: dataNoordHolland, zeeland: dataZeeland
The format of this file is:
- Type of car
- Which province
- Data refer to this province.
Then I made this small parser to parse it as an array:
for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { var data = {}, provinces = roadTaxData.provinceWeightFuelPricesData[vehicleType]; for (var province in provinces) { var provinceData = provinces[province]; data[province] = []; for (var provinceDataIndex = 0; provinceDataIndex < provinceData.length; provinceDataIndex++) { data[province].push(provinceData[provinceDataIndex]); } console.log('Parsed a the province: ' + province + " from the vehicle type " + vehicleType); console.log(''); } console.log('Parsed the vehicle type: ' + vehicleType); console.log(''); console.log(data); passToPHP(vehicleType, JSON.stringify(data)); }
This is all fine, and I am returning the correct data array when I do this:
console.log(data);
But when I passed it to PHP using this method:
function passToPHP (paramName, data) { if (typeof paramName === "string" && typeof data === "string") { var httpc = new XMLHttpRequest(); // simplified for clarity" httpc.open("POST", INSTALL_FILE, true); // sending as POST httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); /* For testing */ httpc.onreadystatechange = function () { //Call a function when the state changes. if (httpc.readyState == 4 && httpc.status == 200) { // complete and no errors console.log(httpc.responseText); // some processing here, or whatever you want to do with the response } }; httpc.send(paramName + "=" + data); } }
With this PHP file:
header('Content-Type: application/json'); $personen_auto = $_POST['personen_auto']; $kampeer_auto = $_POST['kampeer_auto']; print_r(json_decode($personen_auto)); print_r(json_decode($kampeer_auto));
First, I get this error, which does not reconfigure the kampeer_auto index from $_POST , which I actually send:
Note : Undefined index: kampeer_auto in C: \ Users \ Bas \ Documents .. \ Cars \ install.php on line 6
Then the data log of the personen_auto object.
Then there is another error with this message that does not redo the personen_auto index, which I also just parsed and printed?
Notification : Undefined index: personen_auto in C: \ Users \ Bas \ Documents .. \ Cars \ install.php on line 5
Questions
- How come they donβt convert these
$_POST variables? - How can I get PHP to only get $ 1 _POST at a time?
My own attempt
I tried using the passPHP () method outside the for loop, for example:
for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { var data = {}, provinces = roadTaxData.provinceWeightFuelPricesData[vehicleType]; for (var province in provinces) { var provinceData = provinces[province]; data[province] = []; for (var provinceDataIndex = 0; provinceDataIndex < provinceData.length; provinceDataIndex++) { data[province].push(provinceData[provinceDataIndex]); } console.log('Parsed the province: ' + province + " from the vehicle type " + vehicleType); console.log(''); } console.log('Parsed the vehicle type: ' + vehicleType); console.log('');
But this only passed one PHP variable (which was kampeer_auto ).