I am new on codeigniter I tried to fetch data which is filter by date from database and display it in my view page through ajax without refreshing my page. I tried lot but didn't get the solution .
Here is my controller:
public function sohan_by_date(){
/* Check the validation of search by date function*/
$this->form_validation->set_rules('start_date', 'Start Date', 'trim|required');
$this->form_validation->set_rules('end_date', 'End Date', 'trim|required');
if($this->form_validation->run()){
/* display data from database from comparing the date in database*/
$date1=$this->input->post('start_date');
$date2=$this->input->post('end_date');
$start_date=date('Y-m-d', strtotime($date1));
$end_date=date('Y-m-d', strtotime($date2));
$check=$this->task->get_by_date_sohan($start_date,$end_date);
if($check){
$data['result']=$this->task->get_by_date_sohan($start_date,$end_date);
$this->load->view('admin/sohan_task_by_date', $data);
return json_encode($data);
}
else{
redirect('task/sohan_task');
}
}
else{
redirect('task/sohan_task');
}
}
Here is my model:
public function get_by_date_sohan($start_date,$end_date){
/* This display all task of sohan from database by date*/
$this->db->select('*');
$this->db->from('task_form');
$this->db->where('name','sohan');
$this->db->where('date >= ',$start_date);
$this->db->where('date <= ',$end_date);
$query=$this->db->get();
return $result=$query->result();
}
Here is my ajax:
<script type='text/javascript' language='javascript'>
$('#getdata').click(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url();?>task/sohan_by_date",
dataType: "json",
success: function(data){
debugger;
$('#result_table').html(data);
},
error: function() { alert("oops..."); }
});
});
</script>
here is my view:
<div class="form-body">
<div data-example-id="simple-form-inline">
<?php
$attributes = array('class' => 'form-inline');
echo form_open('', $attributes);
?>
<div class="form-group">
<input type="text" class="form-control" name="start_date" id="start_date" placeholder="Start Date" >
</div>
<div class="form-group">
<input type="text" class="form-control" name="end_date" id="end_date" placeholder="End Date">
</div>
<button type="submit" class="btn btn-default" id="getdata">Check</button>
<?php
echo form_close();
?>
</div>
</div>
<div id='result_table'>
<?php include('date.php');?>
</div>
My date.php file is:
<table class="table table-striped table-hover table-responsive">
<thead>
<tr class="success">
<th>Name</th>
<th>Date</th>
<th>Work</th>
<th>Partner</th>
<th>Director</th>
<th>Time</th>
<th>Task</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
<td><?php echo $row->date;?></td>
<td><?php echo $row->work;?></td>
<td><?php echo $row->partner;?></td>
<td><?php echo $row->director;?></td>
<td><?php echo $row->time;?></td>
<td><?php echo $row->task;?></td>
<td><?php echo $row->status;?></td>
</tr>
<?php } ?>
</tbody>
</table>
When I am fetching data normally without any using ajax and display in view then it working properly but page is refreshing.It means my controller and my model is working properly.But when I used ajax to display data without refreshing page then its not working ..please help me to find the solution .I dont have much more knowledge in ajax.
echo json_encode($data);instead ofreturn json_encode($data);in your controller file.result_tableon your table, the problem might also come from here. Could you try and add it ? BTW, if by accessingyoursite.com/task/sohan_by_datein your browser outputs a JSON strong, then the problem isn't the controller or the ajax call, but the parsing of the return string.