Sponsored Links

What's news? Follow my updates on Twitter! | Advertise on Woork

Friday, February 1, 2008

Search with autosuggest feature

8 comments
Save to del.icio.us 0 hit!

Are you an Ajax newbie and are you looking for a simple way to implement autosuggest feature for your PHP site?

This tutorial explains how to implement a simple PHP ajax search with autosuggest feature using some lines of PHP and Javascript code. The result is like this:


How it works
The package contains:

- index.php (search form)
- config.php (MySQL connection's parameters. Change them!)
- search.php (PHP code to execute query)
- ajax_framework.js (javascript functions to enable ajax feature)

When an user type a string in the input field with id "search-q" the function autosuggest() searches the current string into a database table...

<input name="search-q" id="search-q" type="text" onkeyup="javascript:autosuggest()"/>
<div id="results"></div>

... and the script shows search results into the <div> with id "result".

All files are fully customizable. Remeber to change connection's parameter in config.php and SQL code for $getRecord_sql query in search.php. I used a generic query in search.php... change the code in bold changing table name (INVENTORY) and table field (TAG):

$getRecord_sql = 'SELECT * FROM INVENTORY WHERE TAG LIKE "'.$searchq.'%"';


Download this tutorial

Subscribe my Feeds RSS in your preferred RSS reader
Comments
8 comments
Anonymous said...

Thanks. I'm searching for this tutorial!

Michael said...

To any one using this please note it does not escape the user input, there for if you use it unmodified you are open to SQL injection and also XSS attacks as the output is not escaped either.
You should use something like mysql_real_escape_string, casting, ctype_* or even regex but please use something.
Other then that nice article :)

Anonymous said...

Hi Antonio, great tutorial! Simple and clear :))

Anonymous said...

I love this, but if you click on one of the result, that result should appear in the textbox, how do i do this?

John Smith said...

Could this work for blogger?

Anonymous said...

great script man, really solid work.

anyone know how to apply arrow keys to highlight the suggestions?
Thanks

Maxchen said...

I received empty field after text-typing. Made all instructions.
What a problem?

lukasz said...

Hi Antonio this is great !
Could you tell us what's the best way to copy result value to a search box???

Facebook | Feeds About Me

Subscribe now!Feeds RSS

Recent Post

Recent Comments