Describes an Ajax auotocomplete framework.

Source: Internet
Author: User

This paper introduces a Ajax auotocomplete framework, works of foreigners, http://www.createwebapp.com,

This is called createwebapp's JavaScript AutoComplete widget. We will introduce it in PHP below.

 

1 download

 

2. Create a table
Create Table 'temp '(
'Id' int (10) unsigned not null auto_increment,
'Content' text not null,
Primary Key ('id ')
) Engine = MyISAM default charset = utf8 auto_increment = 1;
Is the keyword table

 

3 search.html

<! 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 = UTF-8"/>
<Title> untitled document </title>
<SCRIPT type = "text/JavaScript" src = "JS/ajax. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "AutoComplete/javascripts/prototype. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "AutoComplete/javascripts/autocomplete. js"> </SCRIPT>
<LINK rel = "stylesheet" type = "text/CSS" href = "AutoComplete/styles/autocomplete.css"/>
<SCRIPT type = "text/JavaScript">

VaR Ajax = new ajaxrequest ();
// Submit the search
Function search (){
VaR element = Document. getelementbyid ("result ");
Ajax. postf (
"P ",
// Function (OBJ) {alert (obj. responsetext )},
Function (OBJ) {element. innerhtml = '<Div class = "Search"> the following are the search results </div>' + obj. responsetext ;},
"Update. php ",
"Post ");
}
// Search for Ajax pages
Function show2 (ADD, page ){
VaR element = $ ("result ");
Element. innerhtml = "Ajax. Get (
"Update. php? Action = SEARCH & add = "+ Add +" & page = "+ page,
Function (OBJ) {element. innerhtml = '<Div class = "Search"> The following is the' + Page + 'page </div>' + obj. responsetext ;})

}
</SCRIPT>
</Head>
<Body onload = "show2 ('', 1) ">
<Form action = "Update. php" method = "Post" id = "p">
<P>
<Input name = "keyword" id = "AJ" style = "margin-top: 20px; width: 250px; Height: 40px;">
</P>
<P>
<Input type = "hidden" name = "action" value = "Search"/>
<Input name = "" type = "button" value = "Submit query" onclick = "Search ()" style = "margin-top: 20px; width: 250px; height: 40px; "/>
</P>
</Form>
<SCRIPT>
New AutoComplete ("keyword", function (){
Return "Update. php? Action = boot & Q = "+ this. value;
});
</SCRIPT>
<Div id = "result"> </div>
</Body>
</Html>

 

3. In the Automatic completion function, you can check whether there are any records in the database. If there is no record, you can increase the number of use times by 1.

<? PHP
Require_once ('Global. php ');
Header ("Content-Type: text/html; charset = UTF-8 ");
If ($ _ post ['action'] = 'save '){
If ($ db-> query ("insert into temp (content) values ('$ content ')")){
Echo "submitted successfully! <A href = \ "update.html \"> return </a> ";
}
} If ($ action = 'search '){
$ Pagenum = 3;
$ Page = intval ($ _ Get ['page']);
If (! Empty ($ page )){
$ Start_limit = ($ page-1) * $ pagenum;
} Else {
$ Start_limit = 0;
$ Page = 1;
}
If (isset ($ _ Get ['add']) {
If (safe_check ($ _ Get ['add']) {
Die ('invalid parameter! ');
} Else {
$ Add = $ _ Get ['add'];
}
} Else {
$ Add = "";
}
If ($ _ post ['keyword']! = ""){
$ Key = $ _ post ['keyword'];
$ OBJ = $ db-> fetch_one_array ("select * From temp1 where keyword = '$ key '");
If (empty ($ OBJ )){
$ Db-> query ("insert into temp1 (keyword, num) values ('$ key', 1 )");
} Else {
$ Old = $ OBJ [num] + 1;
$ Db-> query ("Update temp1 set num = '$ old' where keyword =' $ key '");
}
$ Add = "where content like '% $ _ post [keyword] % '";
}
$ Query = $ db-> query ("select * From TEMP $ add order by id desc limit $ start_limit,". $ pagenum );
$ Tatol = $ db-> num_rows ($ db-> query ("select * From TEMP $ add order by id desc "));
$ Page_page = page_search ($ tatol, $ pagenum, $ page ,"");
If (! $ Tatol ){
Die ("No information found ");
}
Print <EOT
<H2> List </H2>
<Ul>
EOT;
While ($ Top = $ db-> fetch_array ($ query )){
$ Temp = trimmed_title (htmlspecialchars (addslashes (str_replace (Array ("\ r \ n", "\ n", "\ r "),'', $ top ['content']), 30 );
Print <EOT
<Li> ($ top [ID]) $ temp </LI>
EOT;
}
Print <EOT
</Ul>
$ Page_page
EOT;
Unset ($ top );
$ Db-> free_result ($ query );
}

If ($ action = 'boot '){
If (isset ($ _ Get ['q']) {
If (safe_check ($ _ Get ['q']) {
Die ('invalid parameter! ');
} Else {
$ Q = $ _ Get ['q'];
$ Q = str_replace ("_", "\ _", $ q );
$ Q = str_replace ("%", "\ %", $ q );
}
}
$ List = array ();
$ Query = $ db-> query ("select * From temp1 where keyword like '% $ q % '");
While ($ temp = $ db-> fetch_array ($ query )){
Print <EOT
<Li onselect = "This. Text. value = '$ temp [keyword]';"> $ temp [keyword] ($ temp [num])
</LI>
EOT;
}
Unset ($ temp );
$ Db-> free_result ($ query );
}

 

 

 

 

 

// Search for paging Functions
Function page_search ($ num, $ perpage, $ curpage, $ mpurl ){
Global $ add;
$ Add = urlencode ($ add );
$ Multipage = '';
$ Mpurl. = strpos ($ mpurl ,'? ')? '& Amp ;':'? ';
If ($ num> $ perpage ){
$ Page = 10;
$ Offset = 5;
$ Pages = @ Ceil ($ num/$ perpage );
If ($ page> $ pages ){
$ From = 1;
$ To = $ pages;
} Else {
$ From = $ curpage-$ offset;
$ To = $ curpage + $ page-$ offset-1;
If ($ from <1 ){
$ To = $ curpage + 1-$ from;
$ From = 1;
If ($ to-$ from) <$ page & ($ to-$ from) <$ pages ){
$ To = $ page;
}
} Elseif ($ to> $ pages ){
$ From = $ curpage-$ pages + $;
$ To = $ pages;
If ($ to-$ from) <$ page & ($ to-$ from) <$ pages ){
$ From = $ pages-$ page + 1;
}
}
}
$ Curpage1 = $ curpage-1;
$ Curpage2 = $ curpage + 1;
$ Multipage = ($ curpage-$ Offset> 1 & $ pages> $ page? "<A href = \" # \ "onclick = \" show2 ('$ add', '1'); Return false; \ "> first page </a> ": ''). ($ curpage> 1? "<A href = \" # \ "onclick = \" show2 ('$ add',' $ curpage1 '); Return false; \ "> previous page </a> ": '');
For ($ I = $ from; $ I <= $ to; $ I ++ ){
$ Multipage. = $ I ==$ curpage? $ I. '':" <a href = \ "# \" onclick = \ "show2 ('$ add',' $ I '); Return false; \ "> [$ I] </a> ";
}
$ Multipage. = ($ curpage <$ pages? "<A href = \" # \ "onclick = \" show2 ('$ add',' $ curpage2'); Return false; \ "> next page </a> ": ''). ($ to <$ pages? "<A href = \" # \ "onclick = \" show2 ('$ add',' $ pages '); Return false; \ "> last page </a> ":'');
$ Multipage = $ multipage? 'Pagination: '. $ multipage :'';
}
Return $ multipage;
}

Function safe_check ($ SQL _str ){
Return eregi ('select | insert | update | Delete | \/\ * | \. \. \/| \. \/| Union | into | load_file | OUTFILE ', $ SQL _str); // Filter
}
?>

 

Note: <li onselect = "This. Text. value = '$ temp [keyword]';"> $ temp [keyword] ($ temp [num])
</LI>
Must meet the usage of the autocomplete framework

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.