javascript - JQuery UI Autocomplete with PHP file source
419
I am trying to use JQueryUI autocomplete, with data coming from a remote source (another php script). Below is the example given in the demos on the JQueryUI website:
<style>
.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
</style>
<script>
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#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 );
}
});
});
</script>
My question is about the PHP script "search.php". Should this script simply return an array?
Answer
Solution:
The response should be a JSON formatted array in either of these two formats:
An Array of Strings:
[ "Choice1", "Choice2" ]
OR
An Array of Objects with label and value properties:
[ { label: "Choice1", value: "value1" }, ... ]