I am trying to pass an array of objects via ajax to my php file. Basically, once it gets to my php file I want to loop through every object in the array, and insert it's properties into a database.
Here is how I set up the array of objects that gets passed:
for(i=1;i<totalRowCount;i++){
let currentRow = table.rows[i];
itemArray.push({
'itemId' : currentRow.cells[0].innerText,
'memo' : currentRow.cells[1].innerText,
'project' : currentRow.cells[2].innerText,
'department' : currentRow.cells[3].innerText,
'location' : currentRow.cells[4].innerText,
'qty' : currentRow.cells[5].innerText,
'price' : currentRow.cells[6].innerText
})
}
Inside of my ajax call, I pass my data. All of the property values I pass in are just single values, except for itemArray which is my array of objects:
$.ajax({
type: 'POST',
url: 'upload.php',
data:{
'firstName' : firstName,
'lastName' : lastName,
'email' : email,
'date' : date,
'vendor' : vendor,
'txnCurrency' : txnCurrency,
'expDate' : expDate,
'vendorDoc' : vendorDoc,
'justification' : justification,
'desc' : desc,
'itemTotals' : itemTotals,
'subTotals' : subTotals,
'transTotal' : transTotal,
'itemArray' : itemArray
},
success: function(data){
alert('hi');
}
});
My issue is I'm really not sure what format the itemArray will be when it gets to the php. I am trying to loop though right now, but I have somehow got it messed up:
foreach($itemArray as $x) {
$sql2 = $mysqli->prepare("INSERT INTO entries (po_id, memo, project_id, department, locn, qty, price) VALUES (?,?,?,?,?,?,?)");
$sql2->bind_param($last_id,$x['memo'],$x['project'],$x['department'], $x['location'], $x['qty'], $x['price']);
$conn->query($sql2);
}
How would I format this so it works? Basically I want to make a separate insert into my database for every object inside of the array.