PHP search box Prompt (auto-complete) instance Code _php instance

Source: Internet
Author: User
Baidu search Everyone in use, when the user input text, the search box automatically prompts the relevant information, strengthen the user experience, is really good, then this effect is how to achieve it?

First look at the effect of the picture bar, so more motivated, otherwise people do not know what I am talking about, in the end to achieve what kind of effect!

Here are the main principles:

In the Search.html page, when the user enters "J" in the search box, use JavaScript to get the text content of the search box, find the relevant content in the database, and then use JavaScript to display the results returned by the server in the prompt box under the search box. For user reference selection.

The specific implementation method:

First in the page to do a search box, search button, display search hints layer, the following code

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

Using the browser to browse the page, you will see the effect of the following figure

Looks ordinary, no style, now a little style adjustment

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

Then we'll adjust the layer style of the search hint, because the search tip layer is just below the search box, so we set it to be absolutely positioned.

#search_auto {border:1px solid #817FB2; Position:absolute}/* Set border, positioning mode/*

Then use JS to place the search hint layer in the search box just below the width and the same search box, where we use jquery to solve

$ (' #search_auto '). css ({' Width ': $ (' #search input[name= ' K '] '). Width () +4});

The location and width of the search hint layer has been determined, because this box is not displayed until the user has entered the search text, so we first want to set it to hidden, add display:none to the style of the hint layer and hide it.

All OK now, as long as the onkeyup of the search box to register the event, we still use jquery to deal with, in jquery is KeyUp

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

The above client is already ready to send the user input to the server and respond to the server's return value.

So how do the server side handle the data sent by the client, and then use PHP to give an example

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

Now the server is ready to execute the data we sent past, and return the corresponding results, then now in the search box to enter a text test, but the premise is that your database has to be related to this text, or you can not see the prompt box appears, because there is no relevant content ah, hehe.

But still a little in the ointment, that is, the contents of the hint box is not beautiful, and we see in Baidu Search in the hint box compared to, is simply too ugly, haha, not urgent, we use CSS to adjust the effect of the display

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

Now is really the complete production, as to whether to set a delay processing, or other more complete functions, leaving friends to their own brains, you can also reply to your ideas below, and so on.

Client complete code:

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

<body>
<div id= "Search" >
<input type= "text" name= "K"/> <input type= "button" name= "S" value= "search"/>
</div>
<div id= "Search_auto" ></div>
</body>

<script src= "Jquery.js" ></script>
<script>
$ (function () {

$ (' #search_auto '). css ({' Width ': $ (' #search input[name= ' K '] '). Width () +4});
$ (' #search input[name= ' K '] '). KeyUp (function () {
$.post (' search_auto.php ', {' Value ': $ (this). Val ()},function (data) {
if (data== ' 0′) $ (' #search_auto '). HTML ("). CSS (' Display ', ' none ');
else $ (' #search_auto '). HTML (data). CSS (' Display ', ' block ');
});
});

});
</script>

Server-Side complete code:

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

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.