首頁  >  文章  >  web前端  >  在jq.ajax+php+mysql中如何實現關鍵字模糊查詢

在jq.ajax+php+mysql中如何實現關鍵字模糊查詢

亚连
亚连原創
2018-06-14 14:11:441662瀏覽

下面我就為大家分享一篇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);
?>

效果圖

#上面是我整理給大家的,希望今後對大家有幫助。

相關文章:

在js中如何實作登入需要滑動驗證

在Angular中如何實作下拉方塊模糊查詢功能

在Nodejs中有關crypto模組安全知識(詳細教學)

#

以上是在jq.ajax+php+mysql中如何實現關鍵字模糊查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn