PHP development: How to implement search keyword prompt function

王林
Release: 2023-09-20 11:16:01
Original
1808 people have browsed it

PHP development: How to implement search keyword prompt function

PHP development: Implementing the search keyword prompt function

The search keyword prompt function is one of the very common and practical functions in modern websites. When the user enters keywords in the search box, the system will provide relevant prompt options based on existing data to facilitate the user's search. This article will use PHP language as an example to introduce how to implement the search keyword prompt function, with specific code examples.

1. Database design

First, you need to design a database table to store keyword data. Taking MySQL as an example, you can create a table called "keywords", which contains two fields: id and keyword. Among them, id is incremented as the primary key, and keyword stores keywords.

2. HTML page

The search keyword prompt function is usually implemented through Ajax technology, so an HTML page containing a search box is required. The following is a simple HTML page example:

<!DOCTYPE html>
<html>
<head>
    <title>搜索关键词提示功能</title>
    <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#search").on("input", function(){
                var keyword = $(this).val();
                $.ajax({
                    url: "search.php",
                    method: "POST",
                    data: {keyword: keyword},
                    success: function(data){
                        $("#keywordList").html(data);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <input type="text" id="search" placeholder="请输入关键词">
    <div id="keywordList"></div>
</body>
</html>
Copy after login

In the above code, listen for searches through $("#search").on("input", function(){}) The input event of the box, when the user enters a keyword, will be sent to the search.php page through an Ajax request, and the keyword will be passed as a parameter.

3. PHP code

In the search.php page, you need to write PHP code to process Ajax requests and query related keyword data from the database. The following is a simple PHP code example:

<?php
// 连接数据库
$connect = mysqli_connect("localhost", "username", "password", "database");

// 检查数据库连接
if(mysqli_connect_errno()){
    echo "无法连接到数据库:" . mysqli_connect_error();
    exit();
}

// 获取关键词参数
$keyword = $_POST['keyword'];

// 从数据库中查询相关关键词数据
$query = "SELECT * FROM keywords WHERE keyword LIKE '%$keyword%'";
$result = mysqli_query($connect, $query);

// 输出相关关键词列表
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo "<p>" . $row['keyword'] . "</p>";
    }
}
else{
    echo "<p>无相关关键词</p>";
}

// 关闭数据库连接
mysqli_close($connect);
?>
Copy after login

In the above code, first connect to the MySQL database through the mysqli_connect() function. Then get the keyword parameters sent through the Ajax request.

Next, query related keyword data from the database through SQL query statements. Using the LIKE statement, you can fuzzy match keywords.

Finally, the data of each row is obtained from the query results through the mysqli_fetch_assoc() function and output to the page.

4. Summary

Through the above HTML page and PHP code examples, we can realize the search keyword prompt function. When the user enters a keyword in the search box, the system will pass the keyword to the PHP script through Ajax technology. The PHP script will query the relevant keyword data from the database and return the result to the HTML page, and finally display it on the page. to users.

Of course, the above examples are for reference only. In actual situations, corresponding changes and optimization may need to be made according to specific needs. However, by understanding the above implementation ideas and code examples, I believe readers can easily implement their own search keyword prompt function.

The above is the detailed content of PHP development: How to implement search keyword prompt function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!