打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Ajax实现搜索功能

      Ajax在搜索功能应用还是比较广泛的,例如百度搜索输入框,输入一个字会跟着显示很多相似的文字。

     接下来是用PHP+MySQL+Ajax实现功能

   

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Ajax实现搜索功能</title>  
  6. <style>  
  7.    #content{  
  8.        width:400px;  
  9.        height:400px;  
  10.        border:2px outset #eeeeee;  
  11.        display:none;  
  12.    }  
  13. </style>  
  14. <script type="text/javascript" src="public.js"></script>  
  15. <script>  
  16. window.onload=function(){  
  17.     $('word').onkeyup=function(){  
  18.         var word=this.value;    //用户输入的内容  
  19.         $('content').innerHTML='';  
  20.         if(word.length==0){  
  21.             $('content').style.display='none';  
  22.             return;  
  23.         }     
  24.         $.get('search.php','word='+word,function(msg){  
  25.             var length=msg.length;  
  26.             if(length>0)  
  27.                 $('content').style.display='block';  
  28.             else  
  29.                 $('content').style.display='none';  
  30.             for(var i=0;i<length;i++){  
  31.                 var name=msg[i].name;   //第i个分类名称  
  32.                 var div=document.createElement('div');  
  33.                 div.onmouseover=function(){  
  34.                     this.style.backgroundColor='#cc6699';  
  35.                 }  
  36.                 div.onmouseout=function(){  
  37.                     this.style.backgroundColor='#ffffff';  
  38.                 }  
  39.                 div.onclick=function(){  
  40.                     $('word').value=this.innerHTML;  
  41.                     $('content').style.display='none';  
  42.                 };  
  43.                 div.innerHTML=name;  
  44.                 $('content').appendChild(div);  
  45.             }  
  46.         },'json');  
  47.     };  
  48. };  
  49. </script>  
  50. </head>  
  51. <body>  
  52. <input type="text" id="word" size="50"/><input type="submit" value="搜索"/>  
  53. <div id="content">  
  54.   
  55. </div>  
  56.   
  57. </body>  
  58. </html>  

接下来用PHP获取数据库中的数据

  1. <?php  
  2.   
  3. $word=$_GET['word'];  
  4. $sql="select name from category where name like '$word%' order by id desc";  
  5. mysql_connect('localhost','root','root');  
  6. mysql_select_db('test');  
  7. mysql_query('set names utf8');  
  8. $result=mysql_query($sql);  
  9. $num=mysql_num_rows($result);  
  10. $data=array();  
  11. for($i=0;$i<$num;$i++){  
  12.     $row=mysql_fetch_assoc($result);  
  13.     $row['name']=iconv('utf-8','utf-8',$row['name']);  
  14.     $data[]=$row;  
  15. }  
  16. mysql_close();  
  17. echo json_encode($data);  

显示效果如下:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
像专业人员一样开发 Ajax 应用程序,第 1 部分: 使用 Prototype Java...
IE6、7下,宽度100%时,滚动条隐显的不可控问题
JavaScript 动画
js基础
JavaScript 创建元素的三种方式
js星星打分效果详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服