i have a array as json.stringify object. the array is from html table.
i have made the array and it made well. i use the code below for create the array and try to getting the array for inserting into my database. i use ajax call function in there
$(document).on('click','#display_data',function(e){
var convertTableToJson = function()
{
var rows = [];
$('.table-bordered tr').each(function(i, n){
var $row = $(n);
rows.push([
$row.find('td:eq(0)').text(),
$row.find('td:eq(1)').text(),
$row.find('td:eq(2)').text(),
$row.find('td:eq(3)').text(),
$row.find('td:eq(4)').text(),
$row.find('td:eq(5)').text(),
$row.find('td:eq(6)').text(),
$row.find('td:eq(7)').text(),
]);
});
return JSON.stringify(rows);
};
$.ajax({
data:convertTableToJson,
type:"POST",
url:"../php/tagihan/save_data.php",
success: function(data){
alert ("Data:" + data);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<table class="table table-bordered">
<thead>
<tr>
<th>Kode Material</th>
<th>Storage Location</th>
<th>Movement Type</th>
<th>Id Indentifier</th>
<th>Item</th>
<th>Date Input</th>
<th>Netto</th>
<th>Unit</th>
<th><input type="checkbox" id="mycheckbox1" name="mycheckbox1"></th>
</tr>
</thead>
<tbody>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006540050</td>
<td>1</td>
<td>10.08.2017</td>
<td>23.970</td>
<td>KG</td>
<td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td>
</tr>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006539985</td>
<td>1</td>
<td>10.08.2017</td>
<td>42.970</td>
<td>KG</td>
<td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td>
</tr>
</tbody>
</table>
<input type="button" id="display_data" name="display_data" />
i don't know why. when i try to run this code with this php function i get an error say's invalid argument of foreach(). and i think the syntax of the foreach that i use is right
the php function that i use for insert the data just like the code below
<?php
include('../../Connections/koneksi.php');
// reading json file
$array = json_decode($_POST['convertTableToJson']);
$data = json_decode($array, true);
foreach ($data as $user) {
$kode = $user[0];
$sloc = $user[1];
$move = $user[2];
$id = $user[3];
$date = $user[4];
$netto = $user[5];
$unit = $user[6];
$payroll = $user[7];
// preparing statement for insert query
$st = mysqli_prepare($db, 'INSERT INTO wjm(kode, sloc, move, id, date, netto, unit, payroll) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'ssssssss', $kode, $sloc, $move, $id, $date, $netto, $unit, $payroll);
// executing insert query
mysqli_stmt_execute($st);
}
?>
please someone help me to solve this.