I want to get information from a database and show it in a tab with Datatables and JQuery on CodeIngniter 3. My table in my view :
<table id="myTable" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Last_name</th>
</tr>
</thead>
</table>
My js script :
$(document).ready(function(){
$('#myTable').DataTable( {
"processing": true,
"serverSide": true,
"order":[],
"ajax": {
"url": '<?= base_url('main/getData');?>',
"type": "POST",
"data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
},
});
})
My controller method :
public function getData()
{
$this->security->get_csrf_token_name();
$this->security->get_csrf_hash();
$table = $this->main_model->data();
if (count($table) > 0)
{
foreach ($table as $row)
{
$tab = array();
$tab["name"] = $row->name;
$tab["last_name"] = $row->last_name;
$r_tab[] = $tab;
}
echo json_encode($r_tab);
}
Thanks a lot :)
<?php echo(find on PHP site reason). Also this line should look like this:"url": "<?php echo base_url('main/getData');?>",. You have issue with quote escaping/ value interpolation here.