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?

844

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" }, ... ]

People are also looking for solutions to the problem: php - Codeigniter upload library get thumb file name

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.