0

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
              }
            });
        });
      }
    });
3
  • You need to use correct keys as in returning json. Commented Jan 15, 2014 at 8:15
  • keys are correct @cubuzoa Commented Jan 15, 2014 at 8:17
  • And also have you checked your response data on firebug? Is it correct and you are not getting any js error right? Commented Jan 15, 2014 at 8:28

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

@ahmadgulzar then you should correct that in the above question, as it still shows contryName, whereas your json response shows countryname.
@ahmadgulzar are you using firebug? can you check, if you are even receiving any response from the server or not?, also try to do something like console.log(data) , so that we can be sure if its just a string or is actually converted to a JS Object.
yes i am receiving the response. when i get the value in alert() then it shows like this [object object]
0

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?

4 Comments

thanks but its not working. it shows the result but in empty lists. i can hover also but these are blank elements
Could you please check your response json keys?
Ok, I updated code. Could you please try again. I forgot to use map
thanks @cubuzoa it worked but there are some errors i corrected. and it worked

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.