I have developed a mysql database and a php code. In php code i am using jQuery (ajax call) to fetch the data from the database. In html file i have printed datatable's table head only. Rest data i want to fetch from the database. code is given:
HTML CODE:
Student IDStudent NameEmail IDMobileStatus
jQuery CODE:
$(document).ready(function() {
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "fetch.php",
"type": "GET",
"datatype": "json"
}
});
});
fetch.php
$connect = mysqli_connect("localhost","root","","lib");
$sql = "SELECT StudentId, FullName, EmailId, MobileNumber, Status FROM tblstudents";
$result = mysqli_query($connect,$sql);
$json_array = array();
while ($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo json_encode($json_array);
?>
Still the data is not printed in the datatable. What changes are necessary in jQuery?
解决方案
I know this is really late, but I used the exact same code as you (thanks for that by the way!), and what made it work for me is to simply add to the JQuery code:
dataSrc = '' (after url = '...')
so that DataTables knows that it's loading an array. Putting that in made the code work fine!