Programming Web with PHP, CSS, Javascript and ∞
Autocomplete for JQuery.js
Ajax Powered Autocomplete Plugin for JQuery.js
- Icon Notifier (Spinner between Ajax calls)
- Fire onAjaxFailure events when an error occurs during Ajax calls
- Be able to set a custom width
- A more flexible and dynamic creation of the autocomplete text field
The object is pretty complete but it could be nice to include more features. Here some of them:
- Dynamic themes. Being able to change the style of the autocomple and dropdown list could be a really nice feature to have.
- Different effects to show/hide the dropdown list (SlideDown|SlideUp) <- This one is easy with JQuery, any volunteers?
How it works
a) Configure the HEAD section of your HTML page:
<br /> <!-- insert the styles of the autocomplete box --></p> <link rel="stylesheet" href="css/autocomplete.css" type="text/css" media="screen" charset="utf-8" /> <!-- javascript libraries required --> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/autocomplete.jquery.js"></script>
b) Create the objects on DOM Load (you can also create them dynamically too):
var json_options = {
script:'test.php?json=true&limit=6&',
varname:'input',
json:true,
shownoresults:true,
maxresults:16,
callback: function (obj) { $('#json_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};
var xml_options = {
script:'test.php?limit=6&', // for the sake of the server test, json var not set
varname:'input',
json:false, // <----------- IMPORTANT
shownoresults:true,
maxresults:16,
callback: function (obj) { $('#xml_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};
$(function(){
$('#test_json').autoComplete(json_options); // json test
$('#test_xml').autoComplete(xml_options); // xml test
});
As you can see on the code, the ‘script’ property of the object is where we write the server page to call via AJAX in order to get the dropdown results. Let’s view the important lines within this test.php page:
// here we collect the POSTED variable // what to search? $input = strtolower( $_GET['input'] ); $len = strlen($input); // how many results we display? $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
Now, the way you find matches from such POSTED variable is up to you, it could be checked against an array (as in the examples included) or against a database by using the LIKE SQL statement.
Wishlist
Expose your ideas on the comments and I will list them in this small section. It seems that this dropdown keeps rocking, so why not make it better. Here goes mine:
- Paging Results
*note from Miroslav -thanks*
When I use only jQuery plugin autocomplete, it doesn’t work. I have to change the position of div with suggestions by using jQuery .css(“left”,pos.left) and .css(“top”, pos.top + something), otherwise it fails on div.left() and div.top() functions, which aren’t defined.
Reason
This is happening because of the problem that some browsers have with CSS properties not specified as it happens with ‘display:’ style attributes. It is not a bug from the code but from the ‘our beloved browsers’. Thank you very much Miroslav for telling us about this bug.
Updates
14-05-2010 Animations are now using easing functions, linear or swing are available when using jQuery only, more are available from http://gsgd.co.uk/sandbox/jquery/easing/ – Martin Gotik
14-05-2010 Resizing of the list (when more or less items are displayed when typing text) is now animated too, I had to remove and add some elements for it to look smooth in all browsers – Martin Gotik
14-05-2010 The list isn’t hidden anymore when doing an AJAX call when the list is visible, looks more smooth IMHO – Martin Gotik
14-05-2010 Alert in AJAX request is disabled as mentioned by Carpes on the blog – Martin Gotik
05-03-2010 Animations are more dynamic now and available for both the hide and show event – Martin Gotink.
05-03-2010 Updated the browsing through the list with the keyboard. Thanks Martin Gotink.
05-03-2010 MaxResults and MaxHeight implemented. Thanks Martin Gotink.
05-03-2010 Tested all changes with jQuery 1.4.1 and 1.4.2 on IE 6/7/8, Opera 10.10/10.50, FF 3.5 and Chrome 4.0. IE took a bit more effort (especially the maxHeight option), but the result is (almost) the same on all browsers, only IE6/7 has some small issues with the scrollbar when maxHeight is set (some strange effects when using the mouse to drag the scrollbar). Thanks Martin Gotink.
05-03-2010 Tagged all my changes with MG in the .js and .css files in the attached .zip file, so you can see what i’ve changed, and why. Thanks Martin Gotink.
21-01-2010 Fixed style positioning. Thanks Konstantin.
IMPORTANT! All changes made by others will maintain their credits from now on. Apologies for those I didn’t include as I couldn’t update this blog as much as I want due to lack of time -Far too many projects under development.
DOWNLOAD
For some reason, some of you guys cannot download (I have downloads manager plugin for wordpress and it seems that is not working properly with this resource). For those having problems to download, please download the plugin here mirror download
IMPORTANT! Demo is within the download Link -thanks Lev for pointing me this misscommunication thing.
I have updated the link so now all of you shouldnt have problems to download. Sorry for the inconvenience.
|
|
download: Ajax Powered AutoComplete for JQuery (139.34KB) added: 17/01/2010 clicks: 2056 description: |








I'm a 38 year old Web Developer working from Ibiza, Balearic Islands, Spain. I am Founder and Lead Developer for
about 5 months ago
just to confirm, the link works after submitting a comment.
about 5 months ago
Hi,
I am using autocomplete to lookup a massive database of names ( > 400k ) by first name and last name.
Though properly indexed, the query sometimes takes long to retrieve the results and seems to hang.
if it takes too long …. is there a way of specifying an “abort” process?
TIA.
about 5 months ago
Hey!
I’m using jQuery 1.4.2. THe Autocompleter only works with <=1.3.2
It is possible to fix this problem so i can run this plugin with the newest jquery release?
Thanks!
about 5 months ago
I will do my best but I am busy as **** due to http://www.bookakar.com
Best
about 5 months ago
I will do my best but I am very busy at the moment due to http://www.bookakar.com
Best
about 5 months ago
I’m using the plugin with jQuery 1.4.1, without any problem.
What autocomplete file did you download? Because the mirror download contains a .js file with invalid characters in the style position fix, as some ppl complain about. Maybe Antonio can fix this?
about 4 months ago
Is done Martin… support for jquery 1.4.2 granted
Regards
about 4 months ago
The donwload link is not working. I can’t get the file, so I am commenting …
about 4 months ago
Sorry to say… I have been hacked and I have no time to update my site… Hope I could fix it soon… Send me your email and I will forward it to you.
Best
about 4 months ago
Do you have example code with this? i”m looking to plug this into ASP.NET however I am quite new to JQuery so seeing an example of this would be beneficial.
Thanks.
about 4 months ago
You just need to change the PHP code on the example into an ASP.NET code that best suits your needs.
about 4 months ago
So then pretty easy:
var json_options = {
script:’ReportSearch.aspx/GetCompletionList?json=true&limit=6&’,
varname:’ctl00_contentModule_txtJQuerySearch’,
json:true,
shownoresults:true,
maxresults:16,
callback: function (obj) { $(‘#json_info’).html(‘you have selected: ‘+obj.id + ‘ ‘ + obj.value + ‘ (‘ + obj.info + ‘)’); }
};
Should be all i need then?
There hsould be something I need to do to bind my textbox to that though? Or is that all?:
about 4 months ago
I got it working for ASP.NET – works well thank you. I did some mod’s to allow more fields to be brought down etc.
however, the big change is how you call the web service.
the script needs to be something like:
script:’ReportSearch.aspx/GetUserList’,
Then, in the options for DoAjaxRequest you need:
url: url,
data:”{‘input’:'” + data + “‘}”,
Where input is the parameter name. This will then hook up to the ASP.NET web service properly.
about 4 months ago
Your zip file does not include the images or the test.php page you talk about. Where can i get these? Cheers
about 4 months ago
Now it does have everything!
Apologize to all of you but I had such a headache with the hacked site! Now all roles… Thanks to all!
about 4 months ago
Hi, I have tested this Autocomplete Plugin for JQuery.js and it works very well.
Congratulations for the code (and also to all the people collaborating with fixes and upates)!
Using JQuery 1.4.2 in PHP and with database access (MySQL). Tested in browsers IE 6 [the only browser/version showing problems: I had to disable a line in "function(status)" in autocomplete.jquery.js which shows "//alert('AJAX error: '+status);"; but it worked properly after that change], Firefox 3.6.2, Chrome 4.1.249, Safari 3.2.1, Opera 9.63, and Netscape Navigator (version 9.0.0.3).
I downloaded the js file itself (without samples and images, posted around Mar-5-2010) and searched in Google for help. Some days till I could make all things work together, but if someone is intererested you can see a working sample here:
http://www.genealogia.net/search.php
(try to type schmit)
Regards and Cheers
about 3 months ago
where’s a demo?
about 1 month ago
Is it works with Classic ASP and Windows server 2003 ?
about 1 month ago
Hi there Thales
Of course it does, as long as the server returned values to the client script are exactly as those provided on the example
about 1 month ago
Can you Help me What Query can i use in ASP page
Exp. Select customername FROM customers Where Customername Like …..?
about 1 month ago
What parameter are you pass to get the results from Query?
Exp. SELECT querypage.asp?q=Mike
Thanks!!
about 1 month ago
Hi Thales,
If you look at the code you will find that the TEST.PHP page receives a couple of parameters:
test.php?json=true&limit=6&
Check test.php and you will see how this file handles the parameters JSON and LIMIT
Regards