您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 赣州分类信息网,免费分类信息发布

jq、ajax、php、mysql实现关键字模糊查询

2024/6/10 5:28:24发布28次查看
本文主要为大家分享一篇jq.ajax+php+mysql实现关键字模糊查询(示例讲解),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。
对于这个功能企业上还算比较实用,推荐给大家;
index.html
<!doctype html> <html> <head> <meta charset="utf-8"> </head> <style> *{margin:0;padding:0;} .text{width:200px;height:30px;line-height:30px;font-size:14px;outline:none;} ul{width:200px;height:auto;border:1px solid #999;border-top:none;} ul li{width:200px;height:30px;line-height:30px;font-size:14px;} li:hover{background:#ddd;} </style> <body> <input type="text" class="text" name="text"> <ul class="sea"></ul> <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script type="text/javascript"> $(".text").bind("input", function() { if($(this).val().length>0){ search(); }else{ $(".sea").html(''); } }) function search(){ $.ajax({ type:"get", url:"sea.php", data:{"text":$(".text").val()}, success:function(response){ //转换成json对象 eval("var json="+response); //console.log(json) var str=""; for(var i=0;i<json.length;i++){ str += "<li>" + json[i].sea + "</li>"; } $(".sea").html(str); } }) } </script> </body> </html>
sea.php
<?php $con = mysqli_connect("localhost","username","password"); if(!$con){ echo "数据库链接失败"; exit; } mysqli_select_db($con,'jwhuang'); mysqli_query($con,'set names utf-8'); $text= isset($_get['text']) ? trim($_get['text']) : ''; $result=mysqli_query($con,"select * from search where sea like '{$text}%' "); $search=array(); while($row=mysqli_fetch_assoc($result)){ //判断是否有对应的数据 if(!$row){ $search=''; exit; }else{ //对查询关键字进行标记 $row['sea'] = str_replace($text, '<font color="red">' .$text. '</font>', $row['sea']); $search[]=$row; } } echo json_encode($search); ?>
效果图
相关推荐:
php关键字标红处理类
使用正则表达式屏蔽关键字的方法
java中关于this关键字的使用详解
以上就是jq、ajax、php、mysql实现关键字模糊查询的详细内容。
赣州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录