1

I have been following this tutorial to create autocomplete functionality in my form.I'm pretty new to codeigniter and jquery. My table name is 'user_profile' I want to autocomplete 'comp_person_name' and value should be 'p_u_id'.

here is my model

public function get_user($q){
    $this->db->select('comp_person_name,p_u_id');
    $this->db->like('comp_person_name', $q);
    $query = $this->db->get('user_profile');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        // $row_set[]=htmlentities(stripslashes($row['comp_person_name']));
         $new_row['label']=htmlentities(stripslashes($row['comp_person_name']));
         $new_row['value']=htmlentities(stripslashes($row['p_u_id']));
        $row_set[] = $new_row; //build an array

      }
      echo json_encode($row_set); //format the array into json data
      }
    }

and here is my controller

 public function get_users(){
        $this->load->model('user_model');
        if (isset($_GET['term'])){
        $q = strtolower($_GET['term']);
        $this->user_model->get_user($q);


            }
        }

and here is function call

$("#partners").autocomplete({
    source: "get_users" ,
     minLength: 0
        }).focus(function(){            
            $(this).trigger('keydown.autocomplete');
        });

& Html code

 <div class="form-group">
     <label class="col-sm-2  control-label">Referral partner </label>
      <div class="col-sm-10">                                   
       <input type="text" id="partners"  name="assign_to" placeholder="Search    partner"  class="form-control"  />
      </div>
   </div>

Problem is when i press keys, it shows name of persons on dropdown and values on input form . How can i get name on input box also.See the pic below

enter image description here

ashkar
  • 300
  • 1
  • 9

1 Answers1

1

I resolved myself with the help of this stackoverflow thread

Edited Javascript to

$("#partners").autocomplete({
             source: "get_users",
             minLength: 2,
        select: function(event, ui) {
            event.preventDefault();
            $("#partners").val(ui.item.label);
            $("#partners-hidden").val(ui.item.value);
            },
        focus: function(event, ui) {
            event.preventDefault();
            $("#partners").val(ui.item.label);
            $("#partners-hidden").val(ui.item.value);
        }
    });

and added a hidden input field in html with id #partners-hidden for storing and submitting value

Community
  • 1
  • 1
ashkar
  • 300
  • 1
  • 9