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. The following is the call code:
Auto.html content:
Copy codeThe 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 codeThe 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.