【发布时间】:2015-02-20 00:01:02
【问题描述】:
我正在创建一个用于计算荷兰道路税的系统,因为我有一些 JS 数组(数据所在的位置),我正在用 JS 解析它们(同时,因为它是相同的)数据格式),然后使用 XMLHttpRequest 对象将其作为 JSON 格式传递给 PHP。
为此,我首先制作了这个数据映射器:
var roadTaxData = {
provinceWeightFuelPricesData: {
personen_auto: {
noord_holland: dataNoordHolland,
zeeland: dataZeeland
//TODO: Add all the provinces with it's data to the personen_auto object
},
kampeer_auto: {
noord_holland: dataNoordHolland2,
zeeland: dataZeeland2
}
}
}
这个格式是:
- 车辆类型
- 哪个省
- 数据属于该省。
然后我制作了这个小解析器来将它解析为一个数组:
/*
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's 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));
}
这一切都很好,当我这样做时,它会返回正确的数组和数据:
console.log(data);
但是当我用这种方法将它传递给 PHP 时:
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);
}
}
使用这个 PHP 文件:
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));
我首先得到了这个错误,它没有从$_POST 重新调整kampeer_auto 索引,我实际上发送了它:
通知:未定义索引:kampeer_auto in C:\Users\Bas\Documents..\Cars\install.php 第 6 行
然后是personen_auto对象的数据日志。
然后这条消息的另一个错误,它没有重新调整 personen_auto 索引,我也刚刚解析并打印出来?
通知:未定义索引:personen_auto in C:\Users\Bas\Documents..\Cars\install.php 行 5
问题
- 它为什么不重新协调那些
$_POST变量? - 我怎样才能让 PHP 在当时只收到 1 个 $_POST 索引?
我自己的尝试
我尝试将 passPHP() 方法放在 for 循环之外,如下所示:
/*
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's 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));
但这仅将一个变量传递给 PHP(kampeer_auto)。
【问题讨论】:
标签: javascript php arrays json javascript-objects