3/3 Populate an AJAX AutoSuggest field on your Web site with myisam_suggest
In “1/3 Implementing an AutoSuggest feature using MySQL fulltext indices” I described how you can use a MyISAM full-text index to extract search words for an AutoSuggest feature. Then, in “2/3 myisam_suggest: an AutoComplete tool for MySQL fulltext indices” I wrote how to install and use myisam_suggest to extract these search words in practice.
Here you will find how to use AJAX AutoSuggest together with my tool “myisam_suggest”.
For a demo, see www.gimpusers.com. Enter some characters (e.g. “brus”) into the search field on the top of the page.
Download
Here: autosuggest-0.1.zip
Requirements:
- running MySQL server with MyISAM database(s)
- at least one table with full-text index
- “myisam_suggest” compiled and working
- AJAX AutoSuggest on your Web space
- autosuggest.php on your Web space (downloaded from above)
Setup
At first you have to configure autosuggest.php. Open with a text editor and adapt it to your needs:
MAX_COMPLETIONS: how many suggestions should be fetched for a string
TABLE_PATH: the path to your MySQL data files (without trailing slash)
You can test the PHP script by calling it directly, like http://yourserver/path/autosuggest.php?format=as&q=tes. format can be “as” for AutoSuggest format or any other value (or empty) for OpenSearch format. “tes” is the query string in this example.
Then include AutoSuggest as described in its documentation. Basically, you only need to include the JS and CSS file in your HTML HEAD section.
Then connect the search field with the AutoSuggest script and autosuggest.php like this:
<div><label></label>
<input id="search_q" name="q" /></div>
</form><script type="text/javascript"><!—
var options = {
script: "/autocomplete.php?format=as&",
varname: "q",
json: true,
maxresults: 10,
cache: false,
shownoresults: false
};
var as_search = new bsn.AutoSuggest(‘search_q’, options);
// —></script>
Also note that you can use my tool to implement AutoSuggest for OpenSearch.
This tool is in a very early state of development (more proof-of-concept) and I don’t work on it full-time. If you have suggestions or patches, please let me know via comment.