我想從我的.js文件傳遞數組到PHP文件。 數組看起來像JS如下:數組從js到php,錯誤計數數組長度
theBlock[0 - 79] with the values color, x and y. So for example
theBlock[10].color or
theBlock[79].x
這個數據傳遞與下面的腳本:
$.ajax({
type: 'POST',
url: 'insert.php',
data: $(theBlock).serialize()
}).done(function (data) {
console.log(data);
}).fail(function() {
alert("Failed");
});
而且PHP是以下幾點:調用時
$newData = $_POST;
echo count($newData["theBlock"]) . "\n";
現在函數激活這個,數據應該傳遞給PHP,然後發回給我的數組長度。好吧,它總是讓我回到「0」。
陣列由以下腳本創建:
變種theBlock = [];
for (c = 0; c < 80; c++) {
theBlock.push({color: 0});
}
之後數組中的如下方式改變:
if (ctx.getImageData(xPixel + minX, yPixel + minY, 1, 1).data[3] == 255 && ctx.getImageData(xPixel + minX, yPixel + minY, 1, 1).data[0] == 000) {
id = (xBlock + (yBlock - 1) * 8) - 1;
theBlock[id] = { color: 1, x: xBlock, y: yBlock };
}
感謝
打印$ _POST的值以查看實際獲得的'var_dump($ _ POST);'。 – 2014-08-30 22:28:43
當我這樣做時,我得到:array(0){} – Paco 2014-08-30 22:31:09
我們可以看到'theBlock'變量的構造函數 – Ohgodwhy 2014-08-30 22:36:56