0

Here is my jQuery:

$(function() {
      $( "#user_role" ).autocomplete({ 
        source: function( request, response ) {
            $.ajax({
                url: "ajax/search_username",
                dataType: "json",
                data: request,
                success: function(data){
                    if(data.response == 'true') {
                       response($data);
                    }
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
            //Do something extra on select... Perhaps add user id to hidden input    
        },

    });
}());

here is my HTML,

<input type="text" id="user_role" name="user_role">

Here is my controller,

function search_username() {

    $keyword=$this->input->get('term');
    $this->load->model('chat_model');
    $data=$this->chat_model->GetRow($keyword);        
    echo json_encode($data);
} 

Here is my model

public function GetRow($keyword) {       

    $this->db->like('user_type', $keyword, 'both');
    return $this->db->get('lc_user_types')->result_array();
}

What I my trying to do is to load data form database using ajax but it's response is no properties but data is already there in table, please anyone help me for get rid of this.

1 Answer 1

1

First of all check this function it will return result or not

public function GetRow($keyword) {       
    $this->db->like('user_type', $keyword, 'both');
   return $this->db->get('lc_user_types')->result_array();


}

If it returning change the function

$(function() {
   $( "#user_role" ).autocomplete({ 
    source: function( request, response ) {
        $.ajax({
            url: "ajax/search_username",
            dataType: "json",
            data: request,
            success: function(data){
                response($.map(data, function (value, key) {
                        return {
                            id:key,
                            label: value,
                            value: value
                        };
                    }));
            }
        });
    },
    minLength: 1,
    select: function( event, ui ) {
        //Do something extra on select... Perhaps add user id to hidden input    
    },

});
}());
Sign up to request clarification or add additional context in comments.

4 Comments

Have you change javascript function. Manage the result array as key value. Ie array([0]=>'sudhir',[1]=>'prasanga')
In response($.map(data, function (value, key) { console.log(value); // check what returns }));
Thanks a lot sudhir.. it's getting
[{"user_type":"Admin","user_type_id":"1"},{"user_type":"Agent","user_type_id":"2"}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.