Need a great E-Commerce website? Please feel free to contact us.
For more information, please click here.
This jQuery plugin will help you to create an AJAX auto-suggest or auto-complete textfield easily. Some features it has are :
Include jQuery and this plugins inside the <head> tag.
<script language="javascript" type="text/javascript" src="js/jquery.js"></script> <script language="javascript" type="text/javascript" src="js/jquery.suggestion.js"></script>
Also include the CSS file for styling the suggestions.
<link rel="stylesheet" type="text/css" href="css/styles.css" />
Prepare the textbox
<input type="text" id="text1" name="text1" />
Initialize the auto-suggest textfield.
<script language="javascript" type="text/javascript">
$("#text1").suggestion({
url:"data.php?chars="
});
</script>This is the example for the server side script using PHP (require PHP JSON extension).
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
$host="localhost";
$username="";
$password="";
$database="sample";
$con=mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($database,$con) or die(mysql_error());
$arr=array();
$result=mysql_query("SELECT * FROM sample WHERE title LIKE '%".mysql_real_escape_string($_GET['chars'])."%' LIMIT 0, 10",$con) or die(mysql_error());
if(mysql_num_rows($result)>0){
while($data=mysql_fetch_row($result)){
// Store data in array
$arr[]=$data[1];
}
}
mysql_close($con);
// Encode it with JSON format
echo json_encode($arr);
?>You can change the suggestions' styles such as the font color, background color, and the selected item color by customizing the CSS file. Here is the content for the CSS file
/* Style For Suggestions */
/*
For creating side border like this
| item 1 |
| item 2 |
*/
.suggestions .suggest_item{
height:20px;
background-color:#EEEEEE;
border-left:1px solid #CCCCCC;
border-right:1px solid #CCCCCC;
}
/*
For creating top border like this
------------
item 1
...
*/
.suggestions .suggest_item.first{
border-top:1px solid #CCCCCC;
}
/*
For creating bottom border like this
...
item 2
------------
*/
.suggestions .suggest_item.last{
border-bottom:1px solid #CCCCCC;
}
/*
For coloring the selected item
*/
.suggestions .suggest_item.selected{
background-color:#999999;
color:#FFFFFF;
cursor:pointer;
}You can also set the suggestions list to appear after typing some characters using minChars option and also specify its width using width option.
Example :
$("#text2").suggestion({
url:"data.php?chars=",
minChars:2,
width:200
});By using the code above, the suggestions list will appear after 2 or more characters are typed. It also has a fixed width (200 pixel).
The demo can be seen here.
This plugin has been tested and works in the following web browsers.





| Attachment | Size |
|---|---|
| 31.22 KB |
Add new comment