jq.ajax+php+mysql实现关键字模糊查询(示例讲解)

对于这个功能企业上还算比较实用,推荐给大家;

index.html

rush:xhtml;"> <Meta charset="utf-8">

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"&gt;
<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 += "

  • " + json[i].sea + "
  • ";
    }

                $(".sea").html(str);
        }
    })

    }

    sea.PHP

    rush:xhtml;"> 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. '',$row['sea']);
    $search[]=$row;

    }

    }
    echo json_encode($search);
    ?>

    效果

    完整代码下载:https://github.com/jwhuang59/Demos/tree/master/search

    以上这篇jq.ajax+PHP+MysqL实现关键字模糊查询(示例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

    相关文章

    IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
    PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
    使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
    Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
    本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
    Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...