jq.ajax+php+mysql实现关键字模糊查询
2017-12-31 16:19
781 查看
对于这个功能企业上还算比较实用,推荐给大家;
index.html
完整代码下载:https://github.com/jwhuang59/Demos/tree/master/search
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); ?>效果图
完整代码下载:https://github.com/jwhuang59/Demos/tree/master/search
相关文章推荐
- jq.ajax+php+mysql实现关键字模糊查询(示例讲解)
- PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能
- PHP+MySQL实现模糊查询员工信息功能示例
- PHP MYSQL实现登陆和模糊查询两大功能
- jq.ajax+php+mysql实现分页显示数据
- jq.ajax+php+mysql实现瀑布流缓冲加载数据
- php+mysql+ajax实现单表多字段多关键词查询的方法
- MySQL单表多关键字模糊查询的实现方法
- autocomplete+PHP+MYSQL的实现模糊查询
- autocomplete+PHP+MYSQL的实现模糊查询
- MySQL单表多关键字模糊查询的实现方法
- mysql+php+ajax实现分页查询(JSON拼接)
- ajax实现模糊查询(关键字描红)
- autocomplete+PHP+MYSQL的实现模糊查询
- MySQL单表多关键字模糊查询的实现
- PHP MYSQL实现登陆和模糊查询两大功能
- 利用php代码实现关键字百度排名批量查询
- 采用Jquery+Ajax+PHP+MySQL实现分类列表管理 - 上篇
- Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
- 【ZT】SQL实现多字段模糊匹配关键字查询