PHP combined with Jquery.autocomplete plug-in to implement the function of input auto completion hint

Source: Internet
Author: User

We have used search capabilities in many projects to help users find the information they want faster and more accurately. This article will introduce how to implement the user input automatic prompt function, just like Google Baidu search engine, when the user input keyword, the input box will have a hint, will be related to the keyword information display for users to choose, enhance the user experience.

This article will use the jquery UI of the AutoComplete plug-in, combined with back-end PHP, the data source through PHP to read the MySQL data table data.

Xhtml

First, you import the jquery library and related UI Plug-ins, as well as CSS.

The

code is as follows:


<link rel= "stylesheet" href= jquery.ui.autocomplete.css "/>"


<script type= "Text/javascript" src= "Js/jquery.js" ></script>


<script type= "Text/javascript" src= "Ui/jquery.ui.core.js" ></script>


<script type= "Text/javascript" src= "Ui/jquery.ui.widget.js" ></script>


<script type= "Text/javascript" src= "Ui/jquery.ui.position.js" ></script>


<script type= "Text/javascript" src= "Ui/jquery.ui.autocomplete.js" ></script>

The JQuery UI plugin can be downloaded on the official web:
Then write an input box in the body:

The

code is as follows:


<input type= "text" id= "key" name= "key"/>

Jquery

The

code is as follows:


$ (function () {


$ ("#key"). AutoComplete ({


Source: "Search.php",


Minlength:2


});


});

A look at it, call the AutoComplete plug-in, the data source from search.php, when the user input 1 characters to call the data source. The Autocomplte plug-in provides several configurable parameters:
Disabled: is not available after the page is loaded, the default is False, this is not necessary to set to true, not much meaning.
Appendto: Enter the Drop-down prompt box to append the element, the default is "body".
Autofocus: The default is False, and when set to True, the Drop-down prompt box will be the first one to be selected.
Delay: Delay time when loading data, default to 300, in milliseconds.
MinLength: A drop-down prompt appears when you enter as many characters as the default is 1.
Position: Defines the position of the Drop-down prompt box.
Source: Defines the data source, the data source must be in JSON form, and this example is the data source obtained by the request search.php.
AutoComplete also offers a number of events and methods, please check its official website for details:

Php

After invoking the AutoComplete plug-in, there is no hint, don't worry, because you need to call the data source.
First we need a table and add the right amount of data to the table, which is structured as follows:

?
1 2 3 4 5 CREATE TABLE ' art ' (' id ' int (one) not null auto_increment, ' title ' varchar (m) not NULL, PRIMARY KEY (' id ')) engine=myis AM DEFAULT Charset=utf8;


Please build your own table and add data to table art.

Search.php realizes the connection to the MySQL database and, based on the input from the front-end user, queries and obtains the matching content in the data table and outputs it in JSON form.

?
1 2 3 4 5 6 7 8 9 10 11-12 Include_once ("connect.php"); Connection Database $q = strtolower ($_get["term"]); Gets the content entered by the user $query =mysql_query ("SELECT * from Art where the title like ' $q% ' limit 0,10"); Querying the database and composing the result set into an array while ($row =mysql_fetch_array ($query)) {$result [] = array (' ID ' => $row [' id '], ' label ' => $row [' t Itle ']); echo Json_encode ($result); Output JSON data

The final output of the JSON data format is:

The

code is as follows:


[{"id": "3", "title": "U4f7fu7528cssu548cjqueryu5236u4f5cu6f02u4eaeu7684u4e0b


u62c9u9009u9879u83dcu5355 "},


{"id": "4", "title": "U4f7fu7528jqueryu548ccssu63a7u5236u9875u9762u6253u5370


u533au57df "}]

At this point, and then test the input, is not see you want the effect of it?
Finally, it is worth mentioning that the AutoComplete plug-in in Firefox has an input bug, input and can not be prompted, the need for a forward space and then backspace to be prompted. There are a lot of students on the Internet to give the solution, but the latest AutoComplete plug-in code look has been refactored, my solution is to add the following code in 133 lines:

?
1 2 3 4 . Bind ("Input.autocomplete", function () {//repair FF does not support Chinese bug self.search (self.item);});

The above mentioned is the entire content of this article, I hope you can enjoy.

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.