0

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 :)

2
  • Is that separated JS file or file inside view PHP file? Btw, don't use short tags. Use <?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. Commented May 27, 2018 at 22:14
  • What is the problem with your current approach? Commented May 28, 2018 at 0:19

2 Answers 2

1

You have to add columns option to your datatable code for the remote data to display properly.

$(document).ready(function(){
$('#myTable').DataTable( {
        "processing": true,
        "serverSide": false,
        "order":[],
        "ajax": {
            "url": '<?php echo site_url('main/getData'); ?>',
            "type": "POST",
             "data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
        },
            'columns': [
      { "data": "name", "name": "name"},
      { "data": "last_name", "name": "last_name"},
    ]
    });
});

In your controller method, your JSON response is structured incorrectly. You need to add your records in 'data' element of your output array.

Updated controller method:

public function getData()
    {
     $data['csrf_token_name'] = $this->security->get_csrf_token_name(); // $data has to be passed as second parameter of $this->load->view(..., ....), 
     $data['csrf_token_hash'] = $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;                
            }

            $output = array(
                "data" =>  $r_tab
            );

            echo json_encode($output);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Also can you post your whole getData(..) code? You can pastebin it if you prefer.
0

HTML

<table id="myTable" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Last_name</th>
        </tr>
    </thead>
</table>

JQuery

 $('#user_table').DataTable( {

        "ajax": {
            "url": '<?php echo site_url('main/getData'); ?>',
            "type": "POST",
            "data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
        },
    } );

Controller

public function getData()
{
    $this->security->get_csrf_token_name();
    $this->security->get_csrf_hash();
    $this->datatables->select('*')
         ->from('table_name')

    echo $this->datatables->generate();
}

Comments

Your Answer

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