1

I want to refresh content of table from database , so when user pressed refresh button I will retrieve data from database via php , and send this data to jquery which will working on insert retrieved data into table ,I can retrieve data successfully but can not insert it in to datatable this is php code which retrieve data from database;

    elseif ($id=='refresh_logs'){

	  $shortname=$_GET['shortname'];
     	$data_array = array();
    $bul="select radacct.UserName,radacct.AcctStartTime,radacct.AcctStopTime,radacct.AcctOutputOctets,radacct.AcctInputOctets,AcctSessionTime,FramedIPAddress,os.mac,nas.shortname,os_platform,acctupdatetime ,RadAcctId from radacct Left join os on os.username=radacct.UserName and os.AcctSessionid=radacct.AcctSessionId left join nas on radacct.NASIPAddress = nas.nasname where nas.shortname='$shortname' limit 1";
	//echo($bul);
	$result2 = $con2->query($bul);
	 while($row = $result2->fetch_assoc()) {
		  $upload=round($row['AcctOutputOctets']/1024/1024,2);
		 
		  $download=round($row['AcctInputOctets']/1024/1024,2);
		 if ($row['AcctSessionTime']<60) {
				 	$sure=$row['AcctSessionTime']." Sec";
				}
		 else {
					$gun=floor($row['AcctSessionTime']/86400);
					$saat=floor(($row['AcctSessionTime'] - $gun*86400)/3600);
					$dakika=floor(($row['AcctSessionTime'] - $gun*86400 - $saat*3600)/60);
					$sure="";
					if ($dakika>0){
					  $sure=$dakika." Min";
	 				}
					if ($saat>0){
	 					$sure=$saat." Hour ".$sure;
					}
					if ($gun>0){
						$sure=$gun." Day ".$sure;
				  }
				}
		 $row['upload']=$upload;
		 $row['download']=$download;
		 $row['sure']=$sure;
	   array_push($data_array,$row);
	   
	   
	   
	   }

			$data_array=json_encode($data_array);	
	echo $format = '{
"data":['.$data_array.']}';
	
}

this is the jquery code to insert retrieved data into table

$('.refresh').click(function(e) {
    e.preventDefault();


    var string ='';
    var nas_shortname=$('#nas_shortname').val();

    var jsonData = $.ajax({ async: false,
      url: "Save.php?id=refresh_logs&shortname="+nas_shortname,

      dataType: "json"

      }).responseText;
        //}

var obj = $.parseJSON(jsonData);
$('#resultt').html('<img src="dist/img/ajax_loader_red_128.gif"/>');
$('#my_logs tbody ').remove();







$.fn.dataTable.ext.errMode = 'throw';
var table = $('#my_logs').DataTable({
"processing": true,
"serverSide": true,
"ajax": obj,
"sScrollY": "300px",
"scrollX":true,
"paging": false,
"bFilter": false,
"bInfo": false,
"searching": false,
"bSort" : false,
"fixedColumns":   true
});


$('#resultt').html('OK');
}); 



});

I have this error in console

DataTables warning: table id=my_logs - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 at J (VM2914 jquery.dataTables.min.js:74)

this is json data which I send from server side to jquery

[{"UserName":"root2","AcctStartTime":"2018-10-18 15:52:52","AcctStopTime":null,"AcctOutputOctets":"0","AcctInputOctets":"0","AcctSessionTime":"0","FramedIPAddress":"192.168.33.3","CallingStationId":"192.168.33.3","shortname":"radius2_nas1","os_platform":null,"acctupdatetime":"2018-10-18 15:52:52","RadAcctId":"1","upload":0,"download":0,"sure":"0 Sec"},{"UserName":"root","AcctStartTime":"2018-10-18 16:07:00","AcctStopTime":null,"AcctOutputOctets":"0","AcctInputOctets":"0","AcctSessionTime":"0","FramedIPAddress":"192.168.33.3","CallingStationId":"192.168.33.3","shortname":"radius2_nas1","os_platform":null,"acctupdatetime":"2018-10-18 16:07:00","RadAcctId":"2","upload":0,"download":0,"sure":"0 Sec"}]

1 Answer 1

1

From PHP side your $data_array should be like below:

Array
(
[draw] => 1
[recordsTotal] => 4827
[recordsFiltered] => 4827
[data] => Array
     (
        [0] => Array
            (
                [UserName] => root2
                [AcctStartTime] => 2018-10-18 15:52:52 
                [AcctStopTime] => 
                [AcctOutputOctets] => 0
                [AcctInputOctets] => 0
                [AcctSessionTime] => 0
                [FramedIPAddress] => 192.168.33.3
                [CallingStationId] => 192.168.33.3
                [shortname] => radius2_nas1
                [os_platform] => 
                [acctupdatetime] => 2018-10-18 15:52:52
                [RadAcctId] => 1
                [upload] => 0
                [download] => 0
                [sure] => 0 Sec
            )
        [1] => Array
            (
                [UserName] => root3
                [AcctStartTime] => 2018-10-18 15:52:52 
                [AcctStopTime] => 
                [AcctOutputOctets] => 0
                [AcctInputOctets] => 0
                [AcctSessionTime] => 0
                [FramedIPAddress] => 192.168.33.3
                [CallingStationId] => 192.168.33.3
                [shortname] => radius2_nas1
                [os_platform] => 
                [acctupdatetime] => 2018-10-18 15:52:52
                [RadAcctId] => 1
                [upload] => 0
                [download] => 0
                [sure] => 0 Sec
            )
        [2] => Array
            (
                [UserName] => root4
                [AcctStartTime] => 2018-10-18 15:52:52 
                [AcctStopTime] => 
                [AcctOutputOctets] => 0
                [AcctInputOctets] => 0
                [AcctSessionTime] => 0
                [FramedIPAddress] => 192.168.33.3
                [CallingStationId] => 192.168.33.3
                [shortname] => radius2_nas1
                [os_platform] => 
                [acctupdatetime] => 2018-10-18 15:52:52
                [RadAcctId] => 1
                [upload] => 0
                [download] => 0
                [sure] => 0 Sec
            )
        ...

     )
)

After json_encode() Your response data have to be as below :

{"draw":1,"recordsTotal":4827,"recordsFiltered":4827,"data":[{"UserName":"root2","AcctStartTime":"2018-10-18 15:52:52","AcctStopTime":null,"AcctOutputOctets":"0","AcctInputOctets":"0","AcctSessionTime":"0","FramedIPAddress":"192.168.33.3","CallingStationId":"192.168.33.3","shortname":"radius2_nas1","os_platform":null,"acctupdatetime":"2018-10-18 15:52:52","RadAcctId":"1","upload":0,"download":0,"sure":"0 Sec"},{"UserName":"root","AcctStartTime":"2018-10-18 16:07:00","AcctStopTime":null,"AcctOutputOctets":"0","AcctInputOctets":"0","AcctSessionTime":"0","FramedIPAddress":"192.168.33.3","CallingStationId":"192.168.33.3","shortname":"radius2_nas1","os_platform":null,"acctupdatetime":"2018-10-18 16:07:00","RadAcctId":"2","upload":0,"download":0,"sure":"0 Sec"}]}

You can also check json is valid or not on https://jsonlint.com/

In case if you don't get success in retrieving serverside datatable as per your need then you can follow Datatable's Serverside example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.