[X]
Thanks for sending this to the admin! We'll take a look at any flagged comments and remove those that shouldn't be here.
What's up with this comment?
(offensive, spam, off-topic, bad grammar :), duplicate, etc.)
Reason:
autoSuggest

MooTools 2 Auto-Suggest

Auto Suggest for MooTools is now on SourceForge! Project tracking, feature requests, bug tracking, etc. can all be seen on the project's homepage: https://sourceforge.net/projects/autosuggestform/


This autoSuggest is built from the ground-up to provide the ability to include key-value pairs for auto-suggest values. How is this useful?
  • To allow users to enter an item into a form in the familiar format but have the form submit the system value; (like an editable select element)
  • To allow users to see only a contextually-relevant subset of the valid values for a form element
  • To remind users of previously-entered values
  • To avoid submitting long, repetitive select lists to forms- resulting in faster page loads
  • and much more!

Navigation:

Example with key->value pairs

This autoSuggest is built to handle key->value arrays (hashes). You will create the visible form element and the hidden form element in the page. Here's an example which displays the 'hidden' form element as an example. Your page can hide the 'hidden' element or not.

What's Your Favorite American State?

Hidden Form Element:
(displayed here for demonstration)

The code to implement this auto-suggest is:
     window.addEvent('domready',function(){
          new autoSuggest('autoSuggest/suggestionServer.php',
               'suggestInput',
               'hiddenOutcome',
               { 'minLength':0, }
          );
	});
			


Example with autoSuggested value only:

The autoSuggest can also be used without a hidden element. When used this way, it is much like google's auto-suggest in that there is no 'hidden' form element to be edited when an item is selected.
What's Your Favorite American State?
The code to implement this auto-suggest is:
     window.addEvent('domready',function(){
          new autoSuggest('autoSuggest/suggestionServer.php', 'suggestInput_alone','');
	});
			


Parameters and Options

Only three parameters are required to instantiate an autoSuggest class:
	window.addEvent('domready',function(){
		new autoSuggest('relative path to ajax script', 'id of visible element','id of hidden element');
	});
			
For instances when there is no hidden element, simply submit a blank third parameter (@see standard autoSuggest Example).

Several options are available to customize the behavior of the autoSuggest class:
Option Default Value Description
method post HTTP Method by which to submit AJAX request
minLength 3 the minimum length (character count in visible input) before requests are sent
cache true cache previous queries (registered per page refresh). When set to true, only one request per case-sensitive query term will be sent
queryTerm query the queryString name to be sent to the remote suggestion server (.../?query=searchForMe)
suggestionClass suggestionClass CSS class used to style the 'li' element containing each suggested item
highlightClass suggestionHover CSS class used to style the 'li' element when it is selected (hover, etc.)
markQuery true Mark the queried portion of the suggestion
queryValueHighlightClass queriedValue CSS class used to mark the queried portion of suggestion, if set to mark query
queryOnFocus false Depending on minLength, send request for suggested values when element fires a focus event


Installing autoSuggest

To install autoSuggest, four files are required: The script and stylesheet links are links to the files running in this page. It is recommended, however, to obtain the latest release from SourceForge. Many thanks to the developers behind the MooTools framework and the JSON parser for making their work freely available!


Change Log

  • Version 0.9.3 (27 August 2009)
    • fixed bug which caused form to be submitted on 'enter' event
  • Version 0.9.2 (27 August 2009)
    • Added support for item selection with 'enter' key
  • Version 0.9.1 (26 August 2009)
    • Added explicit support for 'tabbing'; the auto-suggest now supports familiar form tabbing action.
    • Fixed bug which prevented removal of list when user clicks away (pseudo-blur)
    • Added support to allow a re-query when user re-clicks into the display input
    • Added support to allow a re-query when vertical arrow keys are pressed and display is not currently shown
  • Version 0.9
    • Initial Public Release


Serving data to autoSuggest

autoSuggest expects data from the server in a particular JSON format. Here's an example of server-side code in PHP which provides this expected format:
$sql = "SELECT id_user, username FROM users WHERE username LIKE '%$query%'";
$results = mysql_query($sql);
foreach($results as $row){
     $value = array();
     $value[$row['id_user']] = $row['username'];
     array_push($suggestions,$value);
}
$retVal['query'] = $query; $retVal['suggestions'] = $suggestions;
header('Content-type: application/json');
echo json_encode($retVal); ?>
Valid JSON would look like this: (query=north)
{"query":"north","suggestions":[{"state_36":"North Carolina"},{"state_37":"North Dakota"},{"state_38":"Northern Marianas Islands"}]}


License

The autoSuggest framework files are released under an MIT-style license (http://www.opensource.org/licenses/mit-license.php). These files include autoSuggest.js and autoSuggest.css; MooTools and the JSON Parser are released and licensed by their own organizations.
18 comments have been posted so far...

Leave a comment:
Name:
Your Thoughts:
Please Type
These Characters:
Please type these characters exactly as you see them.
What People have already said:
1. Eric said:
hey, nice code, i hacked at it a bit to get it working with asp.net callbacks instead of mootools callbacks, got it working now pulling data from sql. XD  July 22, 2009 (08:11:39 AM) flag as inappropriate
 
2. Jesse said:
This is a really great chunk of code and it is easy to use for a javascript noob like myself, however I was wondering if there is a way to have it send back more then two callbacks. For example 3 hidden fields. I'm tring to make an application that uses this script to search a name and pull data associated with that name off a DB and put several blocks of data into multiple text fields. Does anyone know if this is possable? -Thanks in advance  June 28, 2010 (06:38:35 AM) flag as inappropriate
 
3. Samir said:
Nice Article .. But how to do same for asp.net????
waiting for ur kind reply...

samir  September 03, 2010 (11:09:58 AM) flag as inappropriate
 
4. anonymous coward said:
Looks great but i dont understand anything of how to serve data to this script. Lets say i have a wordpress blog and i want the data to be served from a database. the database will gradually grow as more tags enter the website and so would the data for the auto suggest. Do you have any tips on how to accomplish this?  October 07, 2010 (11:33:43 AM) flag as inappropriate
 
5. Matthew said:
@jessee: the script doesn't handle that by default, but it shouldn't be too hard to hack it to handle a couple of fields. You'll need to serve them as objects as part of the suggestions array.
@samir: the example is in PHP, of course, but the only changes that would need to be made for any other language would be to serve JSON content that matches the target format.
@ac: the back-end code should contain all of the logic to serve the appropriate suggestions. If you use a tag table, then yes, the table will grow as your database grows. This means that your script will need to have logic to determine which suggestions to serve, either by publish date, relevancy, or some other metric.  December 11, 2010 (08:54:24 AM) flag as inappropriate
 
6. Miguel said:
Great Script, however I seem to be having an issue with it working with any of the Internet Explorers, Error is: Line: 148,Char: 3,Error: Object Expected  February 24, 2011 (04:05:10 AM) flag as inappropriate
 
7. Matthew said:
@Miguel- It's hard to say exactly what the problem is; I can see it working in the IE on my own system. I'd be happy to take a quick look at your sourcecode if you can provide it. If so, send me a message through the contact link...  February 27, 2011 (11:40:04 AM) flag as inappropriate
 
8. Miguel said:
@Matthew yea that would be great if you could please,I have sent you an email via your contact form. cheers  March 01, 2011 (07:06:04 AM) flag as inappropriate
 
9. Esther said:
Stands back from the keyboard in amaezment! Thanks!  December 21, 2011 (06:33:27 PM) flag as inappropriate
 
10. Dayanara said:
In the complicated world we live in, it's good to find simlpe solutions.  December 22, 2011 (02:32:34 AM) flag as inappropriate
 
11. mnqrhcudoek said:
RSsV84 keigxfnwruhx  December 22, 2011 (03:08:11 AM) flag as inappropriate
 
12. Janess said:
Good job making it apepar easy.  December 22, 2011 (06:41:20 AM) flag as inappropriate
 
13. lkkioyzaf said:
xydkOX , [url=http://snkrgvtalbxu.com/]snkrgvtalbxu[/url], [link=http://jzoryqbkzklp.com/]jzoryqbkzklp[/link], http://nkltwhancpvb.com/  December 23, 2011 (04:58:26 AM) flag as inappropriate
 
14. axkxfbrxsju said:
kjqjsn bibepnpepsjp  December 23, 2011 (07:31:49 AM) flag as inappropriate
 
15. Kaylynn said:
That's a sbulte way of thinking about it.  December 23, 2011 (06:33:19 PM) flag as inappropriate
 
16. thjksd said:
FpX4oW hpriaihdopuc  December 24, 2011 (02:16:00 AM) flag as inappropriate
 
17. khloqwz said:
ecMLBG , [url=http://opkgqzvvooth.com/]opkgqzvvooth[/url], [link=http://szoqyiauflww.com/]szoqyiauflww[/link], http://sawlqtdfverx.com/  December 24, 2011 (05:48:34 AM) flag as inappropriate
 
18. qjomprpv said:
C2Tp7p , [url=http://qbyixshxkkhi.com/]qbyixshxkkhi[/url], [link=http://zjlaksfucxpz.com/]zjlaksfucxpz[/link], http://ytvoxemrlbgy.com/  December 24, 2011 (06:58:16 AM) flag as inappropriate