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
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.
Hope this helps you.
Your URL is wrong, you should :
And in addHistory.php, you send data with post name “user_id”. But, in Drivers.php that post name doesn’t exist