Ahmad Gulzar
Ahmad Gulzar

Reputation: 363

jquery autocomplete not displaying the values of array

hi i am using jquery autocomplete with codeigniter my reslut array is coming from database and i am sending back the array using json_encode(). but in response i am not able to show the values in the search but i get the empty reslut, means i get values but i am not able to display but values are coming i can see the empty lis, my data array is like this

[{"countryid":"1","countryname":"Afghanistan"},{"countryid":"2","countryname":"Albania"},{"countryid":"3","countryname":"Algeria"},{"countryid":"4","countryname":"American Samoa"},{"countryid":"5","countryname":"Andorra"}]

my model function is like this

public function search_countries($string)
    {
        $this->db->like('countryname', $string);
        $query = $this->db->get('countries');
        return $query->result_array();
    }

my controller function is this

public function search_countries()
    {
        $string = $this->input->post('countryname');
        $data = $this->user_m->search_countries($string);
        $this->output->set_header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data);
    }

and here is jquery function

$( ".country" ).autocomplete({
      source: function( request, response ) {
        $.post("signup/search_countries", {countryname: request.term}, function(data){
            response(data, function( data ) {
              return {
                label: data.countryid,
                value: data.countryname
              }
            });
        });
      }
    });

Upvotes: 0

Views: 188

Answers (2)

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

I am only giving js part;

$( ".country" ).autocomplete({
      source: function( request, response ) {
        $.post("signup/search_countries", {countryname: request.term}, function(data){
            response($.map( data, function( item ) {
              return {
                label: item.countryname,
                value: item.countryid
              }
            }));
        });
      }
    });

Coudl you please try this?

Upvotes: 0

linuxeasy
linuxeasy

Reputation: 6499

Shouldn't this be:

            label: data.countryid,
            value: data.countryName

as :

            label: data.countryid,
            value: data.countryname

going by your actual json response that you are receiving (change Name to name in countryName).

Upvotes: 2

Related Questions