user4157552
user4157552

Reputation:

jQuery Autocomplete doesn't work (JSON PHP to js)

I have to make AutoComplete with Countries from SQL.

1. php $sql_list_countries=(SQL request)

var_dump

array (size=2)
  0 => 
    object(stdClass)[3]
      public 'meta_value' => string 'United States' (length=13)
  1 => 
    object(stdClass)[4]
      public 'meta_value' => string 'Germany' (length=7)
  1. script javascript (at php file):

var country_array = ;

var test = JSON.stringify (country_array);

console.log (test);

 [{"meta_value":"United States"},{"meta_value":"Germany"}]

3. js file (!)

jQuery(document).ready(function( $ ) {

console.log (ff);

jQuery( '#city_form' ).autocomplete({

source: ff

});

 [{"meta_value":"United States"},{"meta_value":"Germany"}]

So as you see js recieve value of ff as array but Autocomplete function doesn't work. Although if I change ff in js with array var ff = [ "ActionScript", "AppleScript", "Asp"] it works.

Upvotes: 0

Views: 181

Answers (1)

Dmitriy Krupin
Dmitriy Krupin

Reputation: 591

You can specify source as

  • An array of strings: [ "Choice1", "Choice2" ]
  • An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

look jquery docs

Upvotes: 2

Related Questions