php - Ajax auto search in codeigniter -


ajax autosearch

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>   <script type="text/javascript">         function ajaxsearch() {             alert('hai');             var input_data = $('#search_data').val();             alert(input_data);              $.ajax({                   type: "post",                   url: "search/auto_search",                   data: {search_data:input_data},                   success: function(data1) {                     alert(data1);                     // return success                     if (data1.length > 0) {                        $('#suggestions').show();                        $('#autosuggestionslist').addclass('auto_list');                        $('#autosuggestionslist').html(data1);                     }                   }               });         } </script>   

controller

public function auto_search() {         $search_data = $this->input->post('search_data');         //print_r($search_data);  die();         $query = $this->search_model->autocomplete($search_data);         // print_r($query);  die();          foreach ($query->result() $row):             echo $row->uid  ;             echo  $row->name ;         endforeach;     } 

model

public function autocomplete($search_data) {  $this->db->select('name');         $this->db->select('uid');         $this->db->like('name', $search_data);         $dt = $this->db->get('tbl_reg', 10);         //print_r($dt); die();         return $dt->result(); } 

when echo out search_data in controller got whole html page whats reason , please provide solution problem?

change in ajax call well.

      <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>        <script type="text/javascript">       function ajaxsearch() {         alert('hai');         var input_data = $('#search_data').val();         alert(input_data);          $.ajax({               type: "post",               url: "/search/auto_search",               data: {search_data:input_data},               success: function(data1) {                 alert(data1);                 // return success                 if (data1.length > 0) {                    $('#suggestions').show();                    $('#autosuggestionslist').addclass('auto_list');                    $('#autosuggestionslist').html(data1);                 }               }           });     } 

change controller following.

     public function auto_search() {     $search_data = $this->input->post('search_data');     //print_r($search_data);  die();     $query = $this->search_model->autocomplete($search_data);     // print_r($query);  die();     $str='';     foreach ($query $row):         $str.=$row->uid  ;         $str.=$row->name ;     endforeach;    echo $str; } 

this work return result in model , not need use $query->result();


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -