Loading Searchbox
9lessons programming blog logo
Monday, June 29, 2009

Facebook like Autosuggestion with jQuery, Ajax and PHP.

28 comments
I received a lot of request from my readers that asked to me how to implement Autosuggestion search with jquery. I love facebook API it's neat, so I had developed Facebook like Autosuggestion user search with jQuery, Ajax and PHP. It's simple and clean just you have to change the database details.

Facebook like Autosuggestion with jQuery, Ajax and PHP.

Take a look at live demo, search word " sri "

Download Script     Live Demo

Download the Script. Edit Config.php change the database details.

Database
create database table with name "test_user_data"
CREATE TABLE test_user_data
(
uid INT AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(25),
lname VARCHAR(25),
country VARCHAR(25),
img VARCHAR(50)
);



Auto.html
contains jquery(javascript) and HTML Code. Take a look at input field class values search
<script type="text/javascript" src="jquery.js"></script>
</script>
$(document).ready(function(){
$(".search").keyup(function()
{
var searchbox = $(this).val();
var dataString = 'searchword='+ searchbox;
if(searchbox=='')
{}
else
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#display").html(html).show();
}
});
}return false;
});
});
</script>

<input type="text" class="search" id="searchbox" />
<div id="display">
</div>

search.php
Contains PHP code. Display search results

<?php

include('config.php');
if($_POST)
{
$q=$_POST['searchword'];
$sql_res=
mysql_query("select * from test_user_data where fname like '%$q%' or lname like '%$q%' order by uid LIMIT 5");
while($row=mysql_fetch_array($sql_res))
{
$fname=$row['fname'];
$lname=$row['lname'];
$img=$row['img'];
$country=$row['country'];
$re_fname='<b>'.$q.'</b>';
$re_lname='<b>'.$q.'</b>';
$final_fname = str_ireplace($q, $re_fname, $fname);
$final_lname = str_ireplace($q, $re_lname, $lname);

?>

<div class="display_box" align="left">
<img src="user_img/
<?php
echo $img; ?>" />
<?php echo $final_fname; ?>&nbsp;
<?php
echo $final_lname; ?><br/>
<?php echo $country; ?>
</div>
<?php

}
}
else
{}
?>

Using Watermark Input plugin
To show information about the contents of a text field.

<script type="text/javascript" src="jquery.watermarkinput.js"></script>
</script>
jQuery(function($){
$("#searchbox").Watermark("Search");
});
</script>

CSS
id #diplay overflow : hidden
*{margin:0px}
#searchbox
{
width:250px;
border:solid 1px #000;
padding:3px;
}
#display
{
width:250px;
display:none;
float:right; margin-right:30px;
border-left:solid 1px #dedede;
border-right:solid 1px #dedede;
border-bottom:solid 1px #dedede;
overflow:hidden;
}
.display_box
{
padding:4px;
border-top:solid 1px #dedede;
font-size:12px;
height:30px;
}
.display_box:hover
{
background:#3b5998;
color:#FFFFFF;
}


if any queries just post a comment.

Related Posts:

Facebook like suggestions with jQuery content appears and disappears.
Facebook like multi Toggle Comment Box with jQuery and PHP.
Submit multiple comment forms with jQuery and Ajax.
Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
28 comments
Yoosuf said...

its cool, but cant navigate via Keyboard, i guess better to use the LIveQuery Plugin

Arjen said...

This looks really good, I can probably use it in one of my projects, or I can try to integrate it with my wordpress blog.

Thanks for sharing this nice code!

haberler said...

wowww very good article . i will use this method in my new website..many many thanks.

Matt said...

The results disappear if you enter a space after 4 characters (search "Alex").

zamalek said...

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a
copy of this lesson on my site here www.7asryat.prnamg.net

www.ashrok.co.cc said...

this is the best tutorial .. i read it along times and i put a
copy of this lesson and all lessons here on my site here
www.ashrok.co.cc

dev on said...

can i put sql in to edurouteand read it from facebook

Myfacefriends said...

hi do you have mootols version of this? because where using mootols and we have problems using jquery in our site. thanks
http://myfacefriends.com

Rius said...

helo bro,
u have treeview load from database?


thanks

Anonymous said...

"The results disappear if you enter a space after 4 characters (search "Alex")" yes it disappear...but its really nice!!!

Anonymous said...

hi
Good example , but i think there click event is missing .

Picas said...

why the click event is missing..??
i stress to make it..

because i newbie in js...

please provide link event too..

Bruno said...

Hi,

Would it be possible to show a spinner or animated .GIF while he searches?

Anonymous said...

I'm using it, thanks I'm from mandasa

Anonymous said...

When converting this to asp. Is it request.form("search") or request.querystring("search")
Nothing works, since I'm using LIKE it will only display the 5 first results in DB.

And how to make the resultlist on top? All page moves down below resultlist for the moment.

www.ismakineleri.net said...

this information most important for me.
thanks...

Anonymous said...

it's cool..
thanks for share..

Kazi Mohammad Ekram said...

Thanks bro for sharing. Very nice article. I am going to use it for one of my project

Anonymous said...

Testing your comment box

Mukhtaruddin said...

Cool script..thanks for your share its free :)

Abdel-Rahman Awad said...

Hi thanks alot for all your efforts

i made a new auto complete plugin for auto complete which is very simillar to the Facebook search check it out

http://blog.halwsa.net/2010/jquery-autocomplete-search/

Cheers

Anonymous said...

how cany i use it using the keyboard?

Alvaro said...

hi, thanks for this script, i have one question. for what says this error :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ebox/public_html/ssss.com/search.php on line 8
?

the line 8, is while... any idea?. thanks for u help

Tutorials99 said...

Thanks for the this, it’s extremely extensive and useful!,do check out the link below

http://www.tutorials99.com

where all tutorials have a Higher page rank and professional.Very helpful for beginners…

Sandis said...

Wow, thank you, this is very useful... I like it very much. Nice job =)

stelios said...

it is easy to make results dissappear when you click somewhere else ?

Senja said...

Is wonderful

Thanks...

Anonymous said...

it only suggest but you can't click the results...

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter