I've just started to look into Datatables jQuery Plugin and I'm having a it of a hard time getting it to work with my current work.
I normally populate tables using an AJAX callback by getting the values I want from an array, querying the database through a PHP script. For what I've read on the datatables website, something similar is possible but I keep getting errors so I'll just post what I've done so far in hopes someone can help me out.
This is how I call the databale with some ajax parameters.
<script>
$(document).ready( function () {
$('#test_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "test.php",
"type": "POST"
},
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "email" }
]
});
} );
</script>
This is what the php side looks like.
$sql = "SELECT * FROM test_table";
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));
$columns = array(
array('db' => $row['id'], 'dt' => 'id'),
array('db' => $row['name'], 'dt' => 'name'),
array('db' => $row['email'], 'dt' => 'email'),
);
echo json_encode($columns);
however, I get an error saying that "data is not defined". (notice. I read the documentation on the datatables website but I did not exactly followed it step by step. I used this as a reference for what I'm trying to accomplish. Datatables Server Side POST
I am probably going all wrong about this but I didn't want to change my code too much, so I tried an approach I thought would work. If anyone could enlighten me on how to populate datatables by querying the database from a php > json array call, I'd be very grateful.
Thanks in advance,
array('data'=> /**/)around it, before eoncoding