Don P
Don P

Reputation: 63567

jQuery UI - autocomplete from a database

What are the steps for having jQuery UI's autocomplete use a database?

Specifically, how do I pass this script the entered value? How does autocomplete receive the script's json?

What I know:

1) Change the 'source option' to a script that queries the database.
2) ?

Current code:

$("#searchInput input").autocomplete({
     source: "script_that_queries_the_db.php"
});

Upvotes: 0

Views: 2312

Answers (2)

Kavi Siegel
Kavi Siegel

Reputation: 2994

step 2? Have your php page mysql_query based on $_GET['term'] and return the results using json_encode.

Edit: Also, make sure the array you pass to json_encode is a flat array, otherwise jQueryUI won't read it as well as we'd like without writing more custom code.

Upvotes: 4

f1sherman
f1sherman

Reputation: 452

The simplest way is to have your server return the results in json. See this example: http://jqueryui.com/demos/autocomplete/#remote

Another way to do it is to make the request and parse the response yourself, by passing a function as source. See this example: http://jqueryui.com/demos/autocomplete/#remote-with-cache

In either case the data passed to autocomplete must be an array of objects each with a label and a value.

Upvotes: 1

Related Questions