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

小云云
풀어 주다: 2018-01-03 09:09:00
원래의
2172명이 탐색했습니다.

本文主要为大家分享一篇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(&#39;&#39;);

	}
})
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,&#39;jwhuang&#39;);
mysqli_query($con,&#39;set names utf-8&#39;);
$text= isset($_GET[&#39;text&#39;]) ? trim($_GET[&#39;text&#39;]) : &#39;&#39;;
$result=mysqli_query($con,"select * from search where sea LIKE &#39;{$text}%&#39; ");
$search=array();

while($row=mysqli_fetch_assoc($result)){
	//判断是否有对应的数据
	if(!$row){
		$search=&#39;&#39;;
		exit;
	}else{
		//对查询关键字进行标记
		$row[&#39;sea&#39;] = str_replace($text, &#39;<font color="red">&#39; .$text. &#39;</font>&#39;, $row[&#39;sea&#39;]);
		$search[]=$row;
		
	}
}
echo json_encode($search);
?>
로그인 후 복사

效果图

相关推荐:

PHP关键字标红处理类

使用正则表达式屏蔽关键字的方法

Java中关于this关键字的使用详解

위 내용은 jq、ajax、php、mysql实现关键字模糊查询의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!