I’m getting an error when trying to setup a datatables with my project.
DataTables warning: table id=DataTables_Table_0 – Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
This is my index.php:
<!DOCTYPE html>
<html>
<head>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/dataTables.bootstrap4.min.css">
<title>Kontak</title>
</head>
<body>
<div class="container mt-5 mb-5">
<div class="row">
<div class="col-12">
<h1>Data Entry</h1>
</div>
</div>
<hr>
<div class="row">
<div class="col-12">
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Tipe</th>
<th scope="col">PBF</th>
<th scope="col">Tanggal</th>
<th scope="col">Nomor Faktur</th>
<th scope="col">Nomor Gudang</th>
<th scope="col">Nilai</th>
<th scope="col">Jenis</th>
<th scope="col">No. SP</th>
<th scope="col">Setor</th>
<th scope="col">No SPJ</th>
<th scope="col">No Verif</th>
<th scope="col">PPK</th>
<th scope="col">Verif</th>
<th scope="col">Bayar</th>
<th scope="col">Aksi</th>
</tr>
</thead>
<tbody>
<!-- List Data Menggunakan DataTable -->
</tbody>
</table>
</div>
</div>
</div>
<hr>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.24/js/dataTables.bootstrap4.min.js"></script>
<script>
$(function(){
$('.table').DataTable({
"processing": true,
"serverSide": true,
"ajax":{
"url": "ajax/ajax_kontak.php?action=table_data",
"dataType": "json",
"type": "POST"
},
"columns": [
{ "data": "no" },
{ "data": "tipe" },
{ "data": "pbf" },
{ "data": "tanggal" },
{ "data": "faktur" },
{ "data": "noGudang" },
{ "data": "nilai" },
{ "data": "jenis" },
{ "data": "sp" },
{ "data": "setor" },
{ "data": "nospj" },
{ "data": "noverif" },
{ "data": "ppk" },
{ "data": "verif" },
{ "data": "bayar" },
{ "data": "aksi" },
]
});
});
</script>
</body>
</html>
this is ajax_kontak.php:
<?php
require_once '../koneksi.php';
if($_GET['action'] == "table_data"){
$columns = array(
0 =>'id_entry',
1 =>'tipe',
2 => 'pbf',
3 => 'tanggal',
4 =>'faktur',
5 =>'noGudang',
6 => 'nilai',
7 => 'jenis',
8 =>'sp',
9 =>'setor',
10 => 'spj',
11 => 'noverif',
12 =>'ppk',
13 =>'verif',
14 => 'bayar',
15 => 'id_entry',
);
$querycount = $mysqli->query("SELECT count(id_entry) as jumlah FROM entry");
$datacount = $querycount->fetch_array();
$totalData = $datacount['jumlah'];
$totalFiltered = $totalData;
$limit = $_POST['length'];
$start = $_POST['start'];
$order = $columns[$_POST['order']['0']['column']];
$dir = $_POST['order']['0']['dir'];
if(empty($_POST['search']['value']))
{
$query = $mysqli->query("SELECT id_entry, tipe, pbf, tanggal,
faktur, noGudang, nilai, jenis,
sp, setor, spj, noverif,
ppk, verif, bayar
FROM entry order by $order $dir
LIMIT $limit
OFFSET $start");
}
else {
$search = $_POST['search']['value'];
$query = $mysqli->query("SELECT id_entry, tipe, pbf, tanggal,
faktur, noGudang, nilai, jenis,
sp, setor, spj, noverif,
ppk, verif, bayar
FROM entry WHERE tipe LIKE '%$search%' or
pbf LIKE '%$search%' or
tanggal LIKE '%$search%' or
faktur LIKE '%$search%' or
noGudang LIKE '%$search%' or
nilai LIKE '%$search%' or
jenis LIKE '%$search%' or
sp LIKE '%$search%' or
setor LIKE '%$search%' or
spj LIKE '%$search%' or
noverif LIKE '%$search%' or
ppk LIKE '%$search%' or
verif LIKE '%$search%' or
bayar LIKE '%$search%'
order by $order $dir
LIMIT $limit
OFFSET $start");
$querycount = $mysqli->query("SELECT count(id_entry) as jumlah FROM entry WHERE tipe LIKE '%$search%' or
pbf LIKE '%$search%' or
tanggal LIKE '%$search%' or
faktur LIKE '%$search%' or
noGudang LIKE '%$search%' or
nilai LIKE '%$search%' or
jenis LIKE '%$search%' or
sp LIKE '%$search%' or
setor LIKE '%$search%' or
spj LIKE '%$search%' or
noverif LIKE '%$search%' or
ppk LIKE '%$search%' or
verif LIKE '%$search%' or
bayar LIKE '%$search%'");
$datacount = $querycount->fetch_array();
$totalFiltered = $datacount['jumlah'];
}
$data = array();
if(!empty($query))
{
$no = $start + 1;
while ($r = $query->fetch_array())
{
$nestedData['no'] = $no;
$nestedData['tipe'] = $r['tipe'];
$nestedData['pbf'] = $r['pbf'];
$nestedData['tanggal'] = $r['tanggal'];
$nestedData['faktur'] = $r['faktur'];
$nestedData['noGudang'] = $r['noGudang'];
$nestedData['nilai'] = $r['nilai'];
$nestedData['jenis'] = $r['jenis'];
$nestedData['sp'] = $r['sp'];
$nestedData['setor'] = $r['nama'];
$nestedData['spj'] = $r['spj'];
$nestedData['noverif'] = $r['noverif'];
$nestedData['ppk'] = $r['ppk'];
$nestedData['verif'] = $r['verif'];
$nestedData['bayar'] = $r['bayar'];
$nestedData['aksi'] = "<a href='#' class='btn-warning btn-sm'>Ubah</a> <a href='#' class='btn-danger btn-sm'>Hapus</a>";
$data[] = $nestedData;
$no++;
}
}
$json_data = array(
"draw" => intval($_POST['draw']),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
}
Tried Running and modificating the columns but nothing works.
the error persists: