I have an issues with datatable sorting and filter basically all the JS function not working.
I've already include the JS files.
some details:
I'm connecting to the database to retrieve data in json.
The php file:('district.php')
<?php
include_once('db.php');
$db = mysql_select_db('mor_app',$con);
if(!$db){ die('Database selection failed '.mysql_error()); }
$sql = 'SELECT *FROM users';
$result = mysql_query($sql,$con);
$data = array();
while($row = mysql_fetch_array($result)){
$row_data = array(
'id' => $row['id'],
'date' => $row['date'],
'username' => $row['username'],
'Q1' => $row['Q1'] );
array_push($data, $row_data); }
echo json_encode($data);
?>
the html file:
<!DOCTYPE html>
<html>
<head>
<!-- DataTables CSS -->
<link rel=stylesheet type=text/css href=//cdn.datatables.net/1.10.0/css/jquery.dataTables.css>
<!-- DataTables -->
<script type=text/javascript charset=utf8 src=//cdn.datatables.net/1.10.0/js/jquery.dataTables.js></script>
<script src=js/jquery-1.9.1.min.js></script>
</head>
<body>
<table id=table_id class=display>
<caption>Try</caption>
<thead> <tr>
<th>ID</th>
<th>Date</th>
<th>user name</th>
<th>Q1</th> </tr>
</thead>
<tbody id=tablebody>
</tbody>
</table>
<script>
$(function() {
var url = 'district.php';
$.getJSON(url, function(data) {
$.each(data, function(index, data) {
$('#tablebody').append('<tr>');
$('#tablebody').append('<td>'+data.id+'</td>');
$('#tablebody').append('<td>'+data.date+'</td>');
$('#tablebody').append('<td>'+data.username+'</td>');
$('#tablebody').append('<td>'+data.Q1+'</td>');
$('#tablebody').append('</tr>');
});
});
});
$('#table_id').DataTable();
</script>
</body>
</html>
now I can see the table but I just can't sorting or searching.
if I write something in the search box the table is empty. the same happen with sorting.
it say: No data available in table.
I thought it's happening because I'm retrieving data. any suggestion?
Thank a lot, Mor