Developing an auto-complete script with AJAX/PHP

Working on on Management system for the Comercial department I received innumerous requests saying “but can’t you show me the client’s name as I type here?”. Well I wish the words were as poetica as those but what really happened is that they were mixing up the registered clients with IE auto-complete feature, well you know, users will be users.

So I was faced with a challenge, how could I solve the problem working is a web-based environment? I needed a script that would search the database in real time as I typed the actual words in the text box, and return to me the vendor related to that client. Well some time back thinking in web-based systems I would just claim “impossible! Dude, we are talking browser here, no some VB or deplhi program!”… not anymore! Hence xmlHttpRequest came into question and the new AJAX-way of working net content came into view.

So I begun my search for an “auto-complete” or “Google suggest” (yeah, when you are good I can create cool names uh?) script that would handle my needs. I found quite a bit each with a diferent solution but no AJAX integrated (except Google’s of course). I tried to get a clue from Google implementation, but trust me, when someone wants to make a javascript file look scrambled and illegible, well it is possible, and Google accomplished just that.

So I begun from the beginning as they say:

The idea was simple, with each keystrike an AJAX request is sent to a PHP script that is responsible for searching the database looking for the entries that begin with the string provided and returning a XML formatted list of results. Something like:

That was simple enough, just trigger a floating DIV and fill it with an unordered list (UL) and feed each result as a lit item (LI). So the basic was there, but you needed to grab the mouse and click on the choosen result when you found it, not that much fun…

It was missing something like Google used, that triggered the navigation through the results using the keyboard, up and down arrows to be exact. And also using the ENTER and TAB keys to select the result. So I based myself on another script I had checked out just to get a heading on which way to go. So adding A plus B I finally finished the script, complete with AJAX, database search, keyboard and mouse navigation… the works. When it was done I found innumerous other applications for it, it seemed this kind of script can be applied in several situations, and make them much more user-friendly. So I decided to make the script more generic to make its distribution easier so I could shared it with all of you.

Live Demo: here Download (with examples): dmsAutoComplete.zip

Hope it turns out to be of some use to any of you.

Known Bugs: In Firefox on the first list population run the UL appears as a single line, not breaking lines between choices.

comments powered by Disqus

Related Posts

4ª Edição da PHP Magazine, e artigo sobre AJAX

4ª Edição da PHP Magazine, e artigo sobre AJAX

  • March 10, 2008

Finalmente saiu a 4ª edição da PHP Magazine, prometida para agosto de 2007.

Read More
Changes on their way

Changes on their way

  • November 22, 2010

So over the next few weeks this space right here is going to be seeing lots of changes.

Read More
dmsLiveList: Lista de itens em Tempo Real com AJAX

dmsLiveList: Lista de itens em Tempo Real com AJAX

  • December 12, 2006

No meu local de trabalho temos um Portal de notícias e uma intranet. Na intranet apresentamos noticias do Portal, como ela esta sendo reformulada, decidi me livrar do iframe e buscar alguma forma de manter uma lista de notícias atualizadas sem que isto ocorra junto a um refresh da página.

Como a necessidade é a mãe de toda invenção, surgiu a idéia de usar AJAX de forma a satisfazer esta minha necessidade. Comecei então este script, simples inspirado na seção “DiggSpy” do Digg.com . Veja como podemos implementar este script no resto do post.

Read More