I need to create a table that is filled with data via ajax with the codes below:
VIEW:
<form class="form-inline" id="form_filter" method="POST">
<label for="date_from">From </label>
<input type="date" name="date_from">
<label for="date_to"> To </label>
<input type="date" name="date_to">
<input type="button" name="btn_view_records" value="Filter Date" class="btn btn-success btn-sm" id="btn_view_records">
</form>
$('#btn_view_records').click(function(){
$.ajax({
type:"POST",
data:$('#form_filter').serialize(),
datataype:"JSON",
url:"<?php echo site_url('pakyaw/get_filtered_data');?>",
success:function(data){
$("#log-list").html(data);
}
});
});
CONTROLLER:
public function get_filtered_data(){
$this->load->library('table');
$this->table->set_heading('Bio Id', 'Log Date', 'Time In', 'Time Out', 'Time Rendendered');
$style = array('table_open' => '<table class="table table-striped table-hover">');
$this->table->set_template($style);
echo $this->table->generate($this->model_pakyaw->get_filtered_data($this->session->userdata('branch_name'), $this->input->post('date_from'), $this->input->post('date_to')));
}
MODEL:
public function get_filtered_data($branch_name, $date_from, $date_to){
$this->output->enable_profiler(TRUE);
$str2="select bio_id as 'bio_id', cast(attendance as date) as 'Log Date', min(attendance) as 'time_in', max(attendance) as 'time_out', timediff(max(attendance), min(attendance)) as 'difference'
FROM delwater_downydb.pakyaw_attendance
WHERE attendance > '".$date_from."'
GROUP BY bio_id, cast(attendance as date)
ORDER BY bio_id, cast(attendance as date) desc;";
$query=$this->db->query($str2);
return $query->result_array();
}
When I click the btn_view_records it doesn't filter the results. I checked and I have seen that the date_from is not being passed. The sql syntax is getting '' value that's why it is not filtering properly. Can you please tell me what I am doing wrong. Thanks.
Typo in your ajax call?
datataype:"JSON",
should be dataType:"JSON",
pass variables assigned to post values or try to print POST values in the model.
print_r($_POST); //in model
$from_date = $this->input->post('date_from');//in model
$date_to= $this->input->post('date_to');//in model
$result = $this->table->generate($this->model_pakyaw->get_filtered_data( $from_date, $date_to, $any_other_variable );// in controller
print_r($result);// in controller