Guys I'm driving crazy with this code: I'm trying to populate my datagrid with the datatable plugin but I'm not understanding why it doesn't working. I have a message whith:
DataTables warning: table id=item_data - Invalid JSON response.
$(document).ready(function() {
$('#item_data').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url:"item_fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[7, 8, 9],
"orderable":false,
},
],
"pageLength": 10
});
});
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<table id="item_data" class="table table-bordered table-striped">
<thead>
<tr>
<th>Item Code</th>
<th>Item Description</th>
<th>Product Line</th>
<th>Purchase Standard Cost</th>
<th>Last Receipt</th>
<th>Status</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<?php //include("item_list.php"); ?>
</table>
Theoretically item_fetch execute the query to populate my datagrid but, the java code doesn't reading the table id. Above my item_fetch code
<?php
//item_fetch.php
include('phpfunc/database_connection.php');
include('phpfunc/function.php');
$query = '';
$output = array();
$query .= "
SELECT * FROM item
INNER JOIN i_prodline ON i_prodline,prodline_id = item.item_prodline
";
if(isset($_POST["search"]["value"]))
{
$query .= 'WHERE i_prodline.prodline_cod LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR i_prodline.prodline_desc LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR item.item_code LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR item.item_desc LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST['order']))
{
$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY item_code DESC ';
}
if($_POST['length'] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$status = '';
if($row['item_status'] == 'active')
{
$status = '<span class="label label-success">Active</span>';
}
else
{
$status = '<span class="label label-danger">Inactive</span>';
}
$sub_array = array();
$sub_array[] = $row['item_code'];
$sub_array[] = $row['item_desc'];
$sub_array[] = $row['prodline_cod'];
$sub_array[] = $row['item_standardcost'];
$sub_array[] = $row['item_datareceipt'];
$sub_array[] = $status;
$sub_array[] = '<button type="button" name="view" id="'.$row["item_id"].'" class="btn btn-info btn-xs view">View</button>';
$sub_array[] = '<button type="button" name="update" id="'.$row["item_id"].'" class="btn btn-warning btn-xs update">Update</button>';
$sub_array[] = '<button type="button" name="delete" id="'.$row["item_id"].'" class="btn btn-danger btn-xs delete" data-status="'.$row["item_status"].'">Delete</button>';
$data[] = $sub_array;
}
function get_total_all_records($connect)
{
$statement = $connect->prepare('SELECT * FROM item');
$statement->execute();
return $statement->rowCount();
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records($connect),
"data" => $data
);
echo json_encode($output);
?>
Please how can I fix it? I'm not understanding where is the mistake.