0

i have the following code:

$(function() {
//autocomplete
$("#autocomplete_ville_nom").autocomplete({
source: "../assets/php_queries/search_ville_nom.php",
minLength: 2
}); 
});

I am trying to tweak in order to have a matching on the first two characters. If the user inputs "An", he should see "Angola" and not "France".

I found the below code that works well:

var wordlist= [ "about", "above", "across", "after", "against",
                "along", "among", "around", "at", "before", 
                "behind", "below", "beneath", "beside", "between", 
                "beyond", "but", "by", "despite", "down", "during", 
                "except", "for", "from", "in", "inside", "into", 
                "like", "near", "of", "off", "on", "onto", "out", 
                "outside", "over", "past", "since", "through", 
                "throughout", "till", "to", "toward", "under", 
                "underneath", "until", "up", "upon", "with", 
                "within", "without"] ; 

$("#input1").autocomplete({
    // The source option can be an array of terms.  In this case, if
    // the typed characters appear in any position in a term, then the
    // term is included in the autocomplete list.
    // The source option can also be a function that performs the search,
    // and calls a response function with the matched entries.
    source: function(req, responseFn) {
        var re = $.ui.autocomplete.escapeRegex(req.term);
        var matcher = new RegExp( "^" + re, "i" );
        var a = $.grep( wordlist, function(item,index){
            return matcher.test(item);
        });
        responseFn( a );
    } });

from jQuery UI Autocomplete widget search configuration

However, i can't find a way to merge both (Basically using my php query and not a list).

Can you please help? thanks

Community
  • 1
  • 1
Harold
  • 23
  • 1
  • 6
  • You can populate an array in JS by using an AJAX/JSON return from PHP. Make that populate your `wordlist` on document ready or on keyup, where ever you prefer. – Derple Jul 12 '14 at 18:18
  • Thanks Stuart but i struggled to create that list using json. could you provide a quick example? thanks – Harold Jul 12 '14 at 23:13
  • This is exactly how to handle what you need for it: http://www.darian-brown.com/pass-a-php-array-to-javascript-as-json-using-ajax-and-json_encode/ – Derple Jul 13 '14 at 09:53

0 Answers0