I am working with AJAX in CodeIgniter.
Here is my code. This is working when my SQL query returns a result. If the query is empty, AJAX doesn't return anything.
My Model:
function view_filter_by_cat($id){
$sql = "SELECT * FROM ".TBL_FILTER_OPTION." WHERE FID=?";
$query=$this->db->query($sql,$id);
if($query->num_rows()){
foreach ($query->result() as $row){
$result[] = $row;
}
$query->free_result();
return $result;
}
AJAX Controller:
public function find_filters_options(){
if($this->input->post('FID')){
$fid = $this->input->post('FID');
$filterList= $this->filter_option_model->view_filter_by_cat($fid);
if($this->filter_option_model->view_filter_by_cat($fid)){
echo (json_encode($filterList));
}else{
echo '0';
}
}
}
Ajax calling:
$.ajax({
type: "POST",
url: '<?php echo site_url('admin/products/find_filters_options'); ?>',
data: {
<?php echo $this->security->get_csrf_token_name(); ?> : '<?php echo $this->security->get_csrf_hash(); ?>',
FID: fid
},
success: function(data1){
alert(data1);
}
});
My question is when the query doesn't return any value, I am unable to access the success message. I want to get the value '0', when the return is empty.