Home Tutorials Fonts Lists Freelance Web Designers List Feed RSSContact Me

Friday, February 1, 2008

Search with autosuggest feature

Save to delicious 0 hits

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

Sponsored Links
Share this post
Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Reddit
Share on Digg
Share on DesignFloat
Share on Technorati
Feeds RSS Subscribe to Feeds RSS
Old 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???

Joarli said...

Hi....2 helps:
1- About the function up/down with keyboard.
2- The result of suggestion, select this on mouse click.
Tank You

LawrencE said...

i need some Autosuggest feature in ASP.NET using C#.NET or any!

If you found any tutorial(s) or you wrote your own tutorial, thanks please reply for this!
I need it in my college project!

thanks in advance :)
regards,
Lawrence. J

Anonymous said...

hi, thank you for your codes and tutorial. it was really helpful. BUT how can i link it to search a particular record? (i.e. the name i click from the list will be used to search from table)

Soso Jana said...

Great ! thank you

About Me

I am Antonio Lupetti, Engineer, Pro Blogger, Mac user, Web addicted. Rome, IT.
Feeds RSS Contact Me

SubscribeFeeds RSS

Contact Me Enter your email address:

SubscribeLatest posts

SubscribeHot Links

Subscribe Design News

Apple Reviews and News
CSSRockstars - You Design, We Code. Your PSD to HTML/CSS for just $149