Baidu search Everyone in use, when the user input text, the search box automatically prompts the relevant information, enhance the user experience, really good, then how this effect is achieved?
First look at it, so more motivated, otherwise we still do not know what I am talking about, in the end to achieve what effect!
Here are the main principles of the first explanation:
In the Search.html page, the user enters "J" in the search box, uses JavaScript to get the text content of the search box, finds the relevant content in the database and returns, and then uses JavaScript to display the results returned by the server in the prompt box below the search box. For user reference selection.
The specific implementation method:
First in the page to do the search box, search button, display search hints layer, the following code
Use the browser to browse the page, you will see the effect
Look normal, no style, now a little bit of style adjustment
#search{font-size:14px;}
#search .k{padding:2px 1px; width:320px;} /*将搜索框宽度设置大点WEB开发笔记(www.chhua.com)*/
Then adjust the layer style that shows the search hint, because the search hint layer is just below the search box, so we set it to be positioned absolutely.
#search_auto {border:1px solid #817FB2; Position:absolute}/* Set border, positioning method */
Then use JS to place the search cue layer is located directly below the search box, and the Width and search box is the same, here 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 the user did not enter the search text before the box is not displayed, so we first set it to hide, in the cue layer style and add display:none to hide it.
Already all OK, now just give the search box onkeyup Register 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 side and respond to the server's return value.
So how does the server handle the data sent by the client, using PHP as an example?
$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 '
';
while($ro=mysql_fetch_array($re)) echo '- '.$ro[title].'
'; //将查询得到的相关结果的标题输出,这个地方需要注意,由于通过jQuery的ajax技术返回的文本是UTF-8编码,所以如果$ro[title] 中包含中文,一定要记得用PHP的iconv或其它函数将其转换成UTF-8编码,否则在页面中看到的会是一串乱码
echo '- 关闭'; //输入一个关闭按钮,让用户不想看到提示层时可以点击关闭,关闭按钮采用jQuery,解释一下,当前点击的按钮是$(this),一直向上找到其第三个父元素,让它逐渐消失WEB开发笔记(www.chhua.com)*/
echo '
';
?>
Now the server has been able to correctly execute the data we sent past, and return the corresponding results, then enter a text in the search box to test it, but the premise is that you have a database with this text-related content Ah, or you can not see the appearance of the cue box, because there is no relevant hint content ah, hehe.
But still a little ointment, that is, the content of the hint box is not beautiful, and we see in Baidu Search in the prompt box compared to, is simply too ugly, haha, not urgent, we then 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 the real complete production, as to whether to set up a delay, or other more perfect function, left to friends to open their brains, you can also reply to your ideas below, and so on.
Client complete code:
jquery+php实现用户输入搜索内容时自动提示
Server-Side complete code:
$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 '
';
while($ro=mysql_fetch_array($re)) echo '- '.$ro[title].'
';
echo '- 关闭';
echo '
';
?>