Parsing a JS array with JS and then passing it to PHP

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 //TODO: Add all the provinces with it data to the personen_auto object }, kampeer_auto: { noord_holland: dataNoordHolland2, zeeland: dataZeeland2 } } } 

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:

 /* Loop through all the specific vehicle types inside the provinceWeightFuelPricesData object */ for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { /* Where the data is getting stored for each vehicle type */ var data = {}, /* Every province with its data contained in the vehicle type */ provinces = roadTaxData.provinceWeightFuelPricesData[vehicleType]; /* Loop through all province with its data in the specific vehicle type */ for (var province in provinces) { /* Define each province data */ var provinceData = provinces[province]; /* Add the province to the object as an key */ data[province] = []; /* Loop through the data which belongs to every province */ for (var provinceDataIndex = 0; provinceDataIndex < provinceData.length; provinceDataIndex++) { /* Add the province data to the array */ 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:

 /* Loop through all the specific vehicle types inside the provinceWeightFuelPricesData object */ for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { /* Where the data is getting stored for each vehicle type */ var data = {}, /* Every province with its data contained in the vehicle type */ provinces = roadTaxData.provinceWeightFuelPricesData[vehicleType]; /* Loop through all province with its data in the specific vehicle type */ for (var province in provinces) { /* Define each province data */ var provinceData = provinces[province]; /* Add the province to the object as an key */ data[province] = []; /* Loop through the data which belongs to every province */ for (var provinceDataIndex = 0; provinceDataIndex < provinceData.length; provinceDataIndex++) { /* Add the province data to the array */ 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(''); //console.log(data); } passToPHP(vehicleType, JSON.stringify(data)); 

But this only passed one PHP variable (which was kampeer_auto ).

+7
json javascript arrays javascript-objects php
source share
3 answers

the code sends only one type of vehicle per call to php. Corresponding code

 for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { data = ... passToPHP(vehicleType, JSON.stringify(data)); } 

The first call passes only "personen_auto" (and kampeer_auto is undefined); the second call passes only "kampeer_auto", and personen_auto - undefined.

The revised version of the code that moves passToPHP outside the loop still resets the data every time through the loop, so the data below will only contain the most recent auto in the province.

To transfer all cars, data must be added (not reinitialized), data must be collected in auto-defined sections (not mixed up), and passToPHP you need to build a multi-parameter query string, one per car. Anything that will significantly rebuild the roadTaxData object.

Or just pass the whole file roadTaxData.provinceWeightFuelPricesData to php and create a php loop and split the auto types.


Edit: you don't need to convert objects to arrays when passing them to php. Php json_decode () can decode objects into associative arrays when the optional second parameter is set to true , like json_decode($data, true) . Just

 passToPHP('json', JSON.stringify(roadTaxData.provinceWeightFuelPricesData)); 

and in php

 $data = json_decode($_POST['json'], true); $kampeer_auto = $data['kampeer_auto']); $personen_auto = $data['personen_auto']); 
+1
source share

try passing the data to php as an unprocessed json string and parse it explicitly with json_decode.

So, send one parameter $ _POST json=string (the string is the urlencoded JSON.stringify data) and has php decoding json_decode($_POST['json'], true) . This should return an array that represents the js object.

+1
source share

Try entering an ad for data outside the loop. It currently gets reset / cleared for each entry in the roadTaxData.provinceWeightFuelPricesData file, so you will always send the last entry to the server.

 var data = {}; for (var vehicleType in roadTaxData.provinceWeightFuelPricesData) { 

Then, as @Andras suggested, you need to decode JSON on the server side:

 $data = json_decode( $_POST['json'], true ); $personen_auto = $data['personen_auto']; $kampeer_auto = $data['kampeer_auto']; 
+1
source share

All Articles