Home php教程 php手册 php 搜索框提示(自动完成)实例代码

php 搜索框提示(自动完成)实例代码

Jun 06, 2016 pm 08:40 PM
search bar automatic completion

输入要搜索的文字时在搜索框下方提示相关的搜索信息实现方法,就是自动完成效果

百度的搜索大家都在用,当用户输入文字时,搜索框下面自动提示相关的信息,加强了用户体验,的确不错,那么这个效果是如何实现的呢

先看一下效果图吧,这样更有动力,要不然大家还不知道我在讲什么,到底要达到什么样的效果!

  下面先主要讲解原理:

  在search.html页面中,用户在搜索框内输入“j”时,使用javascript获取搜索框的文本内容,到数据库中查找相关的内容并返回,再使用javascript将服务器返回的结果显示在搜索框下面的提示框内,供用户参考选择。

  具体的实现方法:

  首先在页面中做好搜索框、搜索按钮、显示搜索提示的层,如下代码

<div id="search"> <br><input type="text" name="k"> <input type="button" name="s" value="搜索"><br> </div> <br><div id="search_auto"></div>

  使用浏览器浏览页面,会看到下图的效果

  看起来很普通,没什么样式,现在稍作样式上的调整

#search{font-size:14px;}<br>#search .k{padding:2px 1px; width:320px;} /*将搜索框宽度设置大点WEB开发笔记(www.chhua.com)*/

  再将显示搜索提示的层样式调整一下,由于搜索提示层在搜索框正下方,所以我们设置其定位方式为绝对定位

  #search_auto{border:1px solid #817FB2; position:absolute;} /*设置边框、定位方式*/

  接着用JS将搜索提示层的位置放置在搜索框正下方,且宽度和搜索框相同,这里我们采用jQuery来解决

  $(‘#search_auto').css({‘width':$(‘#search input[name="k"]‘).width()+4});

  搜索提示层的位置和宽度已经确定好了,由于在用户没有输入搜索文字前这个提示框是不显示的,所以我们先要将它设置成隐藏,在提示层的样式里加上display:none将其隐藏。

  已经全部OK了,现在只要给搜索框的onkeyup注册事件即可,我们依然采用jQuery来处理,在jQuery中是keyup

$('#search input[name="k"]').keyup(function(){<br>$.post('search_auto.php',{'value':$(this).val()},function(data){  //向服务器上的search_auto.php发送post数据,$.post是jQuery的方法<br>if(data=='0') $('#search_auto').html('').css('display','none');  //判断服务器上返回的数据,如果等于0,则表示没有找到相关的内容,所以将提示框的内容清空并隐藏WEB开发笔记(www.chhua.com)*/<br>else $('#search_auto').html(data).css('display','block');  //如果服务器上返回的数据不等于0,则将返回的内容放到提示框内并显示提示框<br>});<br>});

  上面客户端已经做好了,已经可以将用户输入的内容发送到服务器端,并响应服务器的返回值。

  那么服务器端如何处理客户端发送来的数据呢,下面用PHP来举个例子

<?php <BR>$v=$_POST[value];<br>$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");  //根据客户端发送来的数据,到数据库中查询10条相关的结果<br>if(mysql_num_rows($re)echo '<ul>';<br>while($ro=mysql_fetch_array($re)) echo '<li><a href="">'.$ro[title].'</a></li>';  //将查询得到的相关结果的标题输出,这个地方需要注意,由于通过jQuery的ajax技术返回的文本是UTF-8编码,所以如果$ro[title] 中包含中文,一定要记得用PHP的iconv或其它函数将其转换成UTF-8编码,否则在页面中看到的会是一串乱码<br>echo '<li> <a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a>& amp; gt;</li>';  //输入一个关闭按钮,让用户不想看到提示层时可以点击关闭,关闭按钮采用jQuery,解释一下,当前点击的按钮是$(this),一直向上找到其第三个父元素,让它逐渐消失WEB开发笔记(www.chhua.com)*/<br>echo '</ul>';<br>?>

  现在服务器已经可以正确的执行我们发送过去的数据了,并且返回相应的结果,那么现在在搜索框内输入一个文字测试一下吧,但前提是你的数据库中得有与这个文字相关的内容啊,要不然你也看不到提示框的出现,因为没有相关提示内容啊,呵呵。

  可是还有点美中不足,那就是提示框里面的内容不美观,和我们在百度搜索中看到的提示框相比,简直是太丑了,哈哈,不急,我们再用css来调整显示的效果

#search_auto li{background:#FFF; text-align:left;} /*设置提示框内的li标签效果*/<br>#search_auto li.cls{text-align:right;} /*设置提示框内的关闭按钮效果*/<br>#search_auto li a{display:block; padding:5px 6px; cursor:pointer; color:#666;} /*设置提示框内li标签下的a标签效果*/<br>#search_auto li a:hover{background:#D8D8D8; text-decoration:none; color:#000;} /*当鼠标移入提示框内时的效果*/

  现在才算是真正的完全制作完成,至于要不要设置一个延迟处理,或是其它更完善的功能,留给朋友们自己开动脑筋了,大家也可以在下面回复你的想法,等等。

  客户端完整代码:

<br><br><style><BR>#search{font-size:14px;}<BR>#search .k{padding:2px 1px; width:320px;}<BR>#search_auto{border:1px solid #817FB2; position:absolute; display:none;}<BR>#search_auto li{background:#FFF; text-align:left;}<BR>#search_auto li.cls{text-align:right;}<BR>#search_auto li a{display:block; padding:5px 6px; cursor:pointer; color:#666;}<BR>#search_auto li a:hover{background:#D8D8D8; text-decoration:none; color:#000;}<BR></style> <br><title>jquery+php实现用户输入搜索内容时自动提示</title> <br>








<script><BR>$(function(){<br><br>$(‘#search_auto').css({‘width':$(‘#search input[name="k"]‘).width()+4});<BR>$(‘#search input[name="k"]‘).keyup(function(){<BR>$.post(‘search_auto.php',{‘value':$(this).val()},function(data){<BR>if(data=='0′) $(‘#search_auto').html(”).css(‘display','none');<BR>else $(‘#search_auto').html(data).css(‘display','block');<BR>});<BR>});<br><br>});<BR></script>

  服务器端完整代码:

<?php <BR>$v=$_POST[value];<br>$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");<br>if(mysql_num_rows($re)echo '<ul>';<br>while($ro=mysql_fetch_array($re)) echo '<li><a href="">'.$ro[title].'</a></li>';<br>echo '<li> <a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a>& amp; gt;</li>';<br>echo '</ul>';<br>?>

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that the win10 search box is grayed out and cannot be used How to solve the problem that the win10 search box is grayed out and cannot be used Jan 03, 2024 am 11:07 AM

When users daily use the win10 search box to search for content and required software, they find the problem that the win10 search box is gray and cannot be used. Generally, it is set to disabled in the computer policy group. Let’s take a look at the solution to the win10 search box that is gray and cannot be used. method. The win10 search box is gray and cannot be used. Solution: 1. Press the win+R keys to open the run and enter gpedit.msc. 2. In the Local Group Policy Editor - select Administrative Templates - Windows Components option. 3. Find the Search-Allow Cortana option. 4. After opening, select Disabled on its page, click OK, and restart the computer.

How to set the Windows 10 search bar to be transparent How to set the Windows 10 search bar to be transparent Jan 09, 2024 pm 09:46 PM

Some users see that the win10 search box is transparent, and they want to have it, but they don’t know how to set it. Now there is a very simple method. Just download the software from the win10 store. Let’s take a look at how to set the win10 search box to be transparent. method. The win10 search box is transparent: 1. Right-click the taskbar and select Search to turn the search box into a search icon. 2. Open the app store. 3. Search for TranslucentTB software. (You can choose to download the Chinese version) 4. After downloading and installing. 5. Open TranslucentTB, which can be seen in the taskbar on the lower right. 6. Set it to fully transparent.

How to implement autocomplete and autofill through vue and Element-plus How to implement autocomplete and autofill through vue and Element-plus Jul 17, 2023 pm 06:45 PM

How to implement auto-complete and auto-fill through Vue and ElementPlus Introduction: In modern web development, form auto-complete and auto-fill are an important functional requirement. It can improve user experience and reduce the tediousness and errors of repeated input. This article will introduce how to use Vue and ElementPlus to implement auto-complete and auto-fill functions, and provide corresponding code examples. 1. What is autocomplete and autofill? Autocomplete (Autocomplete): When the user enters a text

How to solve the problem of unresponsive search box in win10 How to solve the problem of unresponsive search box in win10 Jan 03, 2024 am 09:44 AM

Users have a stuck problem when using Win10 search box, resulting in the search function being unusable. As long as they are forced to shut down and restart, how to solve this problem? Let's take a look at the solutions to the stuck Win10 search box! The win10 search box is stuck: 1. It gets stuck after clicking search. It may be that it is not responding. You can wait for a while. 2. If it is still stuck after waiting for a while, right-click the taskbar to open the Task Manager and restart Windows Explorer. 3. Open search settings, turn off safe search under permissions and history, and turn off cloud search.

How to disable the search box in win10 taskbar How to disable the search box in win10 taskbar Dec 28, 2023 am 08:17 AM

The win10 taskbar search box is a very easy-to-use search tool, but many users are not used to it or cannot use it. If they want to turn it off, they only need to right-click on a blank space on the taskbar and find the search function to turn it off. How to close the search box on the win10 taskbar 1. First, right-click on a blank space on the taskbar, as shown in the figure: 2. In the interface, click the "Search" option and select Hide, as shown in the figure: 3. Complete the above settings. The search box is hidden. PS: If the search box cannot be closed, it may be a problem with your system, and you need to reinstall the system to solve it.

How to handle autofill and autocomplete in PHP forms How to handle autofill and autocomplete in PHP forms Aug 11, 2023 pm 06:39 PM

How to Handle Autofill and Autocomplete in PHP Forms As the Internet develops, people increasingly rely on autofill and autocomplete features to simplify their operations on the website. Implementing these functions in PHP forms is not complicated. This article will briefly introduce how to use PHP to handle auto-fill and auto-complete of forms. Before we begin, we need to clarify what autofill and autocomplete are. Autofill refers to automatically filling in the fields in a form for users based on their previous input or history. For example, when the user enters an email

How to create a search box with dynamic effects using HTML, CSS and jQuery How to create a search box with dynamic effects using HTML, CSS and jQuery Oct 25, 2023 am 10:28 AM

How to create a search box with dynamic effects using HTML, CSS, and jQuery In modern web development, a common need is to create a search box with dynamic effects. This search box can display search suggestions in real time and automatically complete keywords as the user types. This article will introduce in detail how to use HTML, CSS and jQuery to implement such a search box. Creating the HTML Structure First, we need to create a basic HTML structure. The code is as follows: &lt;!DOCT

How to top the search box on Amap How to top the search box on Amap Feb 29, 2024 pm 09:52 PM

When using the Amap software, we can set up the function of displaying the search box at the top of the page. Let me introduce the operation method for you. Friends who want to know more, come and take a look with me. After opening the Amap software on your mobile phone, click the "My" button in the lower right corner of the page to switch. Then, find the "Settings" icon in the upper right corner of the "My" page and click to open it. 2. After coming to the settings page, find the "Home Page Settings" item and click on it to enter. 3. There is a "Search box at the top" icon at the bottom of the home page settings page. After clicking on it to select it, a "Set up successfully" prompt will pop up on the page. After returning, you can see the display at the top of the home page. There is a search box.

See all articles