1

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.

1 Answer 1

5

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

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

3 Comments

I have my JSON formatted like this which is correct: [{"name":"Thomas","id":1}] how does it workout to put the ?term=ai on the end? Is there a way I can debug the response too?
Using php you do $_GET["term"] to get the querystring, then you do a database query and the results you serialize in Json. Here is an example. fromvega.com/wordpress/2007/05/05/…
I'm getting somewhere now... can see bullet points appearing but no text for the suggestions.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.