0

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.

5
  • I expect auto complete relies on the data being in alphabetical order, your var_dump shows the PHP array isn't sorted - try sorting it before post. Commented Jan 9, 2015 at 21:23
  • as @litechip have pointed, your data is wrong, you should read the doc more carefully. It accepts object in a format [ { label: "Choice1", value: "value1" }, ... ] Commented Jan 9, 2015 at 21:25
  • and you pass ["label": "value"] which is wrong Commented Jan 9, 2015 at 21:26
  • @Rudu I have sorted it array (size=2) 0 => object(stdClass)[3] public 'meta_value' => string 'Germany' (length=7) 1 => object(stdClass)[4] public 'meta_value' => string 'United States' (length=13) But it doesn't help. Commented Jan 9, 2015 at 21:29
  • @user907860 It not request but respond from server. Commented Jan 9, 2015 at 21:30

1 Answer 1

2

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

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

Comments

Your Answer

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