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

PHP Conference Brasil 09 - Quarta

PHP Conference Brasil 09 - Quarta

  • November 25, 2009

Um dia e meio antes da iniciar oficialmente a conference deste ano, começo meu relato sobre o dia a dia da conference em formato de video, seguindo os exemplos de nomes como Cal Evans e Eli White.

Read More
The IDEs of March

The IDEs of March

  • March 15, 2012

Last Year, Chris Shiflett started the “Ideas of March” movement, and Jon Tangerine quickly coined the “Ides of March” twist and some people followed him.

Read More
I Encontro|PHPSP e seu real significado

I Encontro|PHPSP e seu real significado

  • February 16, 2009

O primeiro encontro do PHPSP foi um sucesso! Não pela número massivo de presentes, perto de eventos como a PHPConf , 25 pessoas é um número pequeno, mas sob a perspectiva de que foram 25 onde só tinhamos espaço para 30, e que este foi um evento gratuito e por isso sem “compromisso”, o número de presentes foi sim incrível.

Read More