php - Codeigniter Ajax not returning any error message if the data is not matched in database -


i working ajax in codeigniter.

here code. working when sql query returns result. if query empty, ajax doesn't return anything.

my model:

function view_filter_by_cat($id){ $sql = "select * ".tbl_filter_option." fid=?"; $query=$this->db->query($sql,$id); if($query->num_rows()){     foreach ($query->result() $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 when query doesn't return value, unable access success message. want value '0', when return empty.

you change controller like:

public function find_filters_options(){   $result = array();     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)){                            $result["got_data"] = "yes";             $result["data"] = $filterlist;                     }else{                           $result["got_data"] = "no";         }     }     else {       $result["got_data"] = "no";     }     echo (json_encode($result)); } 

and jquery ajax

$.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 },   datatype: "json",   success: function(data1) {     if(data1.got_data == "yes") {         var data = data1.data; //get data server here     }      else {        //dont have data        //show appropriate message     }   } }); 

Comments