Ajax technical practice-Ajax Automatic completion Function

Source: Internet
Author: User
I recently learned about Ajax and made a few examples to find it novel.

The first is the Autocomplete function, the specific example can be seen here: http://www.b2c-battery.co.uk

Enter a product model in the Search box to see the effect.

Here we use an open-source code: AutoAssist. If you are interested, you can take a look.

The following is a code snippet:

Index.htm
<SCRIPT type = "text/JavaScript" src = "javascripts/prototype. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "javascripts/autoassist. js"> </SCRIPT>
<LINK rel = "stylesheet" type = "text/CSS" href = "styles/autoassist.css"/>
<Div>
<Input type = "text" name = "keyword" id = "keyword"/>
<SCRIPT type = "text/JavaScript">
Event. Observe (window, "LOAD", function (){
VaR AA = new autoassist ("keyword", function (){
Return "forcsv. php? Q = "+ this.txt box. value;
});
});
</SCRIPT>
</Div>

I don't know why I can't use keywords as the text box name. I tried it for a long time. Later I used keyword to modify the original code.

Forcsv. php
<? PHP
$ Keyword = $ _ Get ['q'];
$ COUNT = 0;
$ Handle = fopen ("products.csv", "R ");
While ($ data = fgetcsv ($ handle, 1000 ))! = FALSE ){
If (preg_match ("/$ keyword/I", $ data [0]) {
If ($ count ++> 10) {break ;}
?>
<Div on_Select = "this.txt Box. value = '<? Php echo $ data [0];?> '; ">
<? Php echo $ data [0];?>
</Div>
<? Php
}
}
Fclose ($ handle );
If ($ count = 0 ){
?>
: (, Nothing found.
<? Php
}
?>

In the original example, CSV files are separated by \ t. We can also use spaces or other separators, depending on your data structure.

Of course, you can read data from the database instead of reading files.

As follows:

Transferred from 81M.net

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.