PHP version of jquery AutoComplete Application

Source: Internet
Author: User

The auto-Prompt function is required in work. Haha, I am a JS dish, So Baidu finds a plug-in developed with jquery AND ITS autocomplete. net version of the Automatic completion function, so down to change to the PHP version, and added support for up/down select text and other features, put up to share, pure physical activity... :)
1. Download jquery library, URL: http://jquery.com /;
2. Download The jquery AutoComplete plug-in or directly use the files provided in my attachment. You need to modify some content of the downloaded files to select text up/down and solve Chinese garbled characters, as mentioned on the internet, encodeuri in the file can be changed to escape, and keydown can be changed to keyup. For the support for selecting up/down texts, see the attachment;
3. Call Code :
Auto.html content: Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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> Automatic Test </title>
</Head>
<Body>
<Input type = "text" name = "keyword" id = "Search" size = "30"/>
<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 other parameters call row [1], the corresponding output format is Sparta | 896
}
Function lookupajax (){
VaR osuggest = $ ("# search") [0]. autocompleter;
Osuggest. findvalue ();
Return false;
}
</SCRIPT>
</Body>
</Html>

Getindex. php Copy code The Code is 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 %' group by shopname order by addtime DESC limit 0, 10 ");
If ($ query)
{
While ($ result = $ db-> fetch_array ($ query ))
{
Echo $ result ['shopname']. "\ n ";
}
}
// Convert the data submitted by JS escape
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 attachment is as follows:
Http://xiazai.jb51.net/200912/yuanma/jquery_autocomplete_php.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.