我试图从javascript获取一些数据到php。这是一个行数可变的表格。所以,我试图让所有的数据在JavaScript中循环,然后将它们发送到PHP。在那里,我将使用另一个循环来保存每一行另一个循环。因此,有代码:从javascript到php的多个数组的阵列
JAVASCRIPT
//---- this is the important section ---- //
var data = new FormData();
var table = [];
for (var i = 1; i <= document.getElementById('nblines').value; i++) {
var date = document.getElementById('date' + i).innerHTML;
if (document.getElementById('l' + i).checked == true) { var type = 1;} else { var type = 2;}
var paiement = document.getElementById('a' + i).innerHTML;
var tps = document.getElementById('b' + i).innerHTML;
var tvq = document.getElementById('c' + i).innerHTML;
var total = document.getElementById('d' + i).innerHTML;
var interet = document.getElementById('e' + i).innerHTML;
var principal = document.getElementById('f' + i).innerHTML;
var balance = document.getElementById('g' + i).innerHTML;
var line = [i,date,type,type,paiement,tps,tvq,total,interet,principal,total];
table.push(line);
};
console.log(table);
data.append('amortTable', table);
------------------------------------------------
| |
| ---- this is the NOT important section ---- |
| |
------------------------------------------------
var token = document.getElementById("csrf_token").content;
var request = new XMLHttpRequest();
request.onreadystatechange = function notify()
{
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
setTimeout(loadPage(page),500);
$.niftyNoty({
type: 'success',
container : 'floating',
html : 'Votre calcul s\'est enregistré avec succès!',
timer : 5000
});
} else {
}
} else {
// still not ready
}
}
request.open("POST", "savecalcul", true);
request.setRequestHeader("X-CSRF-TOKEN", token);
request.send(data);
PHP
foreach ($request->input('amortTable') as $i) {
$line = new Amortissement;
$line -> calcul() -> associate($calcul->id);
$line -> number = $i[0];
$line -> date = $i[1];
$line -> type = $i[2];
$line -> paiement = $i[3];
$line -> tps = $i[4];
$line -> tvq = $i[5];
$line -> total = $i[6];
$line -> interet = $i[7];
$line -> principal = $i[8];
$line -> total = $i[9];
$line -> save();
}
我收到此错误local.ERROR:异常 'ErrorException' 有消息“的foreach为无效的论点提供() '在...线是:
foreach ($request->input('amortTable') as $i) {
那么,我是在正确的方式,如果是的话,我在做什么错了。如果我的方式不正确,我该怎么办?
是的,刚刚看到它。谢谢。现在我在console.log中得到了一些信息,但是我在php中遇到了一个错误: local.ERROR:exception'ErrorException'带有'foreach()'提供的无效参数... –
上面的PHP代码不完整。该错误意味着'$ request-> input('table')'不是一个数组。 – rationalboss
您将“JSData”作为“amortTable”添加到“FormData”中。您可能需要将' - > input('table')'更改为' - > input('amortTable')',以便它符合您发送的内容。 (或更改JS使用''表') – Terminus