Reputation: 13080
I'm trying to write a JQuery autocomplete script which will call a url via AJAX and update autocomplete results as user enters data into the form.
I have my AJAX setup and currently returning JSON. But I don't know how to go about getting the autocomplete function to call it and use the response. I have managed to get the following working, but this is static data, so no good for my task:
$("input#name").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
Cheers.
Upvotes: 1
Views: 11068
Reputation: 2404
http://jqueryui.com/demos/autocomplete/#remote
$( "#birds" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
The php needs to return values in Json format like this http://jqueryui.com/resources/demos/autocomplete/search.php?term=ai
Json instructions https://www.php.net/json
Upvotes: 5