jquery+php automatically prompts _php tutorials when users enter search content

Source: Internet
Author: User
today suddenly want to do a search page, so that users can search to find their favorite content, but also to avoid the trouble of manually finding resources in the vast amount of information, my goal and Baidu homepage effect similar, when the user input to search for text, we give the relevant 10 information below, If the user is looking for one of these 10 messages, then simple, directly click on the page can be opened in the new page, the main thing is to be more humane, so that users more convenient to use.

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;} /*将搜索框宽度设置大点*/

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,则表示没有找到相关的内容,所以将提示框的内容清空并隐藏
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),一直向上找到其第三个父元素,让它逐渐消失
      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
;
?>

http://www.bkjia.com/PHPjc/486018.html www.bkjia.com true http://www.bkjia.com/PHPjc/486018.html techarticle today suddenly want to make a search page for this site, so that users can search to find their favorite content, but also to avoid the huge amount of information manually find the resources of the trouble, my goal and ...

  • Related Article

    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.