Sylvie Yunjae Sylvie Yunjae - 6 months ago 66
PHP Question

DataTables 1.10 parse aadata from json failed on HTML5

I'm currently working with HTML5, so I need to use DataTabels 1.10

The problem is the ajaxsouce failed to parse the json from the php file I have.
Actually it worked fine, and still works fine with my project on DataTables 1.9.4 and HTML4, but somehow I failed using it with DataTables 1.10 and HTML5

this is how I call my ajax :

$(document).ready(function() {
$('#example').dataTable( {
"ajax": "viewobat1.php"
} );


} );

I've changed to
"sAjaxSource": "viewobat1.php"
too, but wont work either

this is what I do in
viewobat.php
to get json

$sql="SELECT * FROM obat";
$hasil=mysql_query($sql);
print("{\"aaData\":[");
$tambahan="\n";
$no=0;
while($cetak=mysql_fetch_array($hasil))
{$no++;
$no;
$idobat=$cetak[0];
$tanggalmasuk=$cetak[1];
$namaobat=$cetak[2];
$jenis=$cetak[3];
$macam=$cetak[4];
$keterangan=$cetak[5];
$stok=$cetak[6];

print($tambahan."[\"$no\","."\"$idobat\","."\"$tanggalmasuk\","."\"$namaobat\","."\"$jenis\","."\"$macam\",
"."\"$keterangan\","."\"$stok\]");
$tambahan=",\n";

}
print("\n]}\n");
?>


I dont see any problem to my json, PHP is returning a valid JSON array
output

but the data won't show on table,

am I missing something ?

~EDITED~

I've changed the way to get the required data with the server-side tutorial from datatables.net ---> SERVER SIDE

my code come like this,

<?php
error_reporting(0);
$table = 'obat';

$primaryKey = 'idobat';
$columns = array(
array( 'db' => 'idobat', 'dt' => 0 ),
array( 'db' => 'tanggalmasuk', 'dt' => 1 ),
array( 'db' => 'namaobat', 'dt' => 2 ),
array( 'db' => 'jenis', 'dt' => 3 ),
array( 'db' => 'macam', 'dt' => 4 ),
array( 'db' => 'keterangan', 'dt' => 5 ),
array( 'db' => 'stok', 'dt' => 6 ),
);

$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'medical',
'host' => 'localhost'
);

require( 'js\DataTables-1.10.2\examples\server_side\scripts\ssp.class.php' );

echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);


and of course... PHP is returning valid data again

sample2

I called ajax the exact same way as the tutorial too...

and of course... the data still won't come out in my table...

so again, am I missing something ?? (O_o)

Answer

Yeey... I found my answer (it's weird answer)

just changed my script to -->

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

and it did all the trick... it's weird, isn't it...? i know..

I dont know why should I use CDN that required http instead and failed call it from my own server using PATH_TO_FOLDER method,

but it works anyway, reference from question --> cant get datatables net working with javascript

enter image description here