skip to Main Content

I’m new to Codeigniter and still trying to understand how its work, I’m trying to send ajax from view addHistory to the function fetchDriversHistory in controller Drivers.php
but I’m not getting a response, I’m getting an error

DataTables warning: table id=manageTable1 - Invalid JSON response 

in a browser->network when I check the response of the request is a returning HTML format of the page for some reason

I have file applicationviewsdriversaddHistory.php that initializes the datatable

 manageTable = $('#manageTable1').DataTable({
 "ajax": {
  "url": "fetchDriversHistory",
  "data": {
       "user_id": <?php echo $id ?>
   }
   },
     'order': []
   });

in applicationcontrollersDrivers.php is locate fetchDriversHistory

 public function fetchDriversHistory()
 {
   $result = array('data' => array());

   $data = $this->model_drivers->fetchDriversHistory($id);

   foreach ($data as $key => $value) {

     $result['data'][$key] = array(
       $value['date_added'],
       $value['daily_rate'],
       $value['dayoff']
     );
   }
   echo json_encode($result);
 }

Model
applicationmodelsModel_drivers.php

    public function fetchDriversHistory($id = null)
{
        $sql = "SELECT driver_id, daily_date, dayoff, date_added FROM addDriverHistory where id = ?";
        $query = $this->db->query($sql, array($id));
        return $query->result_array();
}

2

Answers


  1. You URL defining in javascript is wrong.
    By default, you need to define URL as https//:<domain.com>/<controller>/<method>/<arguments>.

    For more deatil read this.

    manageTable = $('#manageTable1').DataTable({
     "ajax": {
      "url": "<?php echo base_url('drivers/fetchDriversHistory');?>", // This will generate absulote URL
      "data": {
           "user_id": <?php echo $id ?>
       }
       },
         'order': []
       });
    

    Hope this helps you.

    Login or Signup to reply.
  2. Your URL is wrong, you should :

    "url": "<?= base_url("your_controller_name/fetchDriversHistory") ?>"
    

    And in addHistory.php, you send data with post name “user_id”. But, in Drivers.php that post name doesn’t exist

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search