In the work need to use the automatic prompt this function, hehe, oneself is JS vegetable, so Baidu a bit, find a with jquery and its plug-in autocomplete development of the. NET version of the automatic completion function, so down to the PHP version, and added support up/ Down to select text and other functions, put up to share, pure physical life ... :)
1, download jquery library, url: http://jquery.com/;
2, download the jquery AutoComplete plug-in or directly using the file provided in my attachment, download the file you need to modify some content to support up/down selection of text, solve Chinese garbled, which solve the problem of Chinese chaos on the internet have mentioned, is to change the file in the encodeURI to escape, there are keydown modified into KeyUp can, as to support up/down selection of text please see annex;
3, the following is the calling code:
Auto.html content:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<script type= "Text/javascript" src= "Include/javascript/jquery.js" ></script>
<script type= "Text/javascript" src= "Include/javascript/jquery.autocomplete.js" ></script>
<link rel= "stylesheet" type= "Text/css" href= "Include/javascript/jquery.autocomplete.css"/>
<title> Auto Completion Test </title>
<body>
<input type= "text" name= "keyword" id= "search" size= "/>"
<script language= "JavaScript" >
$ (document). Ready (function () {
$ ("#search"). AutoComplete (
"Getindex.php",
{
Delay:10,
Minchars:1,
Matchsubset:1,
Matchcontains:1,
Cachelength:10,
Onitemselect:selectitem,
Onfindvalue:findvalue,
Formatitem:formatitem,
Autofill:false
}
);
});
function Findvalue (LI) {
if (Li = null) return alert ("No match!");
if (!! Li.extra) var svalue = li.extra[0];
else var svalue = Li.selectvalue;
}
function SelectItem (LI) {findvalue (LI);}
function Formatitem (row) {return Row[0];//return row[0] + "(ID: + row[1] +") "///if there are other parameters called Row[1], the corresponding output format sparta|896
}
function Lookupajax () {
var osuggest = $ ("#search") [0].autocompleter;
Osuggest.findvalue ();
return false;
}
</script>
</body>
getindex.php
Copy Code code as follows:
<?php
Header ("contenttype:text/plain;charset:gb2312");
Define (' Scriptnav ', ' getindex ');
Require_once './include/common.inc.php ';
$keyWord =iconv (' utf-8 ', ' gb2312 ', Js_unescape ($q));
$query = $db->query ("Select DISTINCT" (Shopname) from {$dbpre}shops WHERE shopname like '% $keyWord% of ' GROUP by Shopname OR DER by Addtime DESC LIMIT 0,10 ");
if ($query)
{
while ($result = $db->fetch_array ($query))
{
echo $result [' Shopname ']. \ n ";
}
}
Convert JS escape submit over data
function Js_unescape ($STR)
{
$ret = ';
$len = strlen ($STR);
for ($i = 0; $i < $len; $i + +)
{
if ($str [$i] = = '% ' && $str [$i +1] = = ' U ')
{
$val = Hexdec (substr ($str, $i +2, 4));
if ($val < 0x7f) $ret. = Chr ($val);
else if ($val < 0x800) $ret. = Chr (0xc0| ( $val >>6)). Chr (0x80| ( $val &0x3f));
else $ret. = Chr (0xe0| ( $val >>12)). Chr (0x80| ( ($val >>6) &0x3f)). Chr (0x80| ( $val &0x3f));
$i + 5;
}
else if ($str [$i] = = '% ')
{
$ret. = UrlDecode (substr ($str, $i, 3));
$i + 2;
}
else $ret. = $str [$i];
}
return $ret;
}
?>
The annexes are as follows:
Http://xiazai.jb51.net/200912/yuanma/jquery_autocomplete_php.rar