search
HomeBackend DevelopmentPHP TutorialPHP combines the jQuery.autocomplete plug-in to implement the function of input automatic completion prompts, jquery.autocomplete_PHP tutorial

PHP combines with the jQuery.autocomplete plug-in to implement the function of inputting automatic completion prompts. jquery.autocomplete

We use the search function in many projects to help users find things faster and more accurately. wanted information. This article will introduce how to implement the function of automatic prompts for user input, just like the Google and Baidu search engines. When the user enters a keyword, there will be a prompt below the input box, displaying information related to the keyword for the user to choose, which improves the user experience. experience.

This article will use the autocomplete plug-in of jquery ui, combined with back-end PHP, and the data source reads the data of the mysql data table through PHP.

XHTML

First import the jquery library, related ui plug-ins, and css.

Copy code The code is as follows:






The jQuery ui plug-in can be downloaded from the official website:
Then write an input box in the body:

Copy code The code is as follows:

jQuery

Copy code The code is as follows:
$(function(){
$("#key").autocomplete({
           source: "search.php",
        minLength: 2
});
});

You can understand at a glance that when the autocomplete plug-in is called, the data source comes from search.php. When the user enters 1 character, the data source is called. The autocomplte plugin provides several configurable parameters:
disabled: Whether it is unavailable after the page is loaded. The default is false. There is no need to set this to true. It does not make much sense.
appendTo: append elements to the drop-down prompt box during input, the default is "body".
autoFocus: The default is false. When set to true, the first drop-down prompt box will be selected.
delay: delay time when loading data, the default is 300, in milliseconds.
minLength: When you enter how many characters, a drop-down prompt will appear. The default is 1.
position: Define the position of the drop-down prompt box.
Source: Define the data source. The data source must be in json form. In this example, the data source is obtained by requesting search.php.
autocomplete also provides many events and methods. For details, please check its official website:

PHP

After calling the autocomplete plug-in, there is no prompt effect yet. Don't worry, because you need to call the data source.
First we need a table and add an appropriate amount of data to the table. The structure of the table is as follows:

CREATE TABLE `art` ( 
 `id` int(11) NOT NULL auto_increment, 
 `title` varchar(100) NOT NULL, 
 PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;


Please create a table by yourself and add data to table art.

search.php implements the connection to the Mysql database, and based on the input of the front-end user, queries and obtains the matching content in the data table, and then outputs it in JSON form.

include_once("connect.php"); //连接数据库 
 
$q = strtolower($_GET["term"]); //获取用户输入的内容 
$query=mysql_query("select * from art where title like '$q%' limit 0,10"); 
//查询数据库,并将结果集组成数组 
while ($row=mysql_fetch_array($query)) { 
  $result[] = array( 
    'id' => $row['id'], 
    'label' => $row['title'] 
  ); 
} 
echo json_encode($result); //输出JSON数据 

The final output JSON data format is:

Copy code The code is as follows:
[{"id":"3","title":"u4f7fu7528CSSu548cjQueryu5236u4f5cu6f02u4eaeu7684u4e0b
u62c9u9009u9879u83dcu5355"},
{"id":"4","title":"u4f7fu7528jQueryu548cCSSu63a7u5236u9875u9762u6253u5370
u533au57df"}]

At this time, test the input again. Do you see the effect you want?
Finally, it is worth mentioning that the autocomplete plug-in has an input bug on Firefox. It does not prompt after typing. You need to space forward and then backspace to prompt. Many students on the Internet have given solutions, but the latest autocomplete plug-in code has been restructured. My solution is to add the following code to line 133:

.bind("input.autocomplete",function(){ 
  //修复FF不支持中文bug 
  self.search(self.item); 
}); 

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/990540.htmlTechArticlePHP combines the jQuery.autocomplete plug-in to implement the function of inputting automatic completion prompts. We use jquery.autocomplete in many projects Search function to help users find what they want faster and more accurately...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function