国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

jq.ajax+php+mysql實現(xiàn)關(guān)鍵字模糊查詢(示例講解)

2024-09-01 08:33:56
字體:
供稿:網(wǎng)友

對于這個功能企業(yè)上還算比較實用,推薦給大家;

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){			//轉(zhuǎn)換成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 "數(shù)據(jù)庫鏈接失敗";	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)){	//判斷是否有對應(yīng)的數(shù)據(jù)	if(!$row){		$search='';		exit;	}else{		//對查詢關(guān)鍵字進行標(biāo)記		$row['sea'] = str_replace($text, '<font color="red">' .$text. '</font>', $row['sea']);		$search[]=$row;			}}echo json_encode($search);?>

效果圖

模糊查詢,關(guān)鍵字,mysql,php,ajax

完整代碼下載:https://github.com/jwhuang59/Demos/tree/master/search

以上這篇jq.ajax+php+mysql實現(xiàn)關(guān)鍵字模糊查詢(示例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 化州市| 斗六市| 康马县| 麻栗坡县| 三都| 巴青县| 郓城县| 九龙县| 黑河市| 启东市| 平原县| 彭泽县| 南昌县| 长宁县| 扶风县| 永德县| 通许县| 淄博市| 东乌| 儋州市| 鄄城县| 阿瓦提县| 永年县| 广灵县| 澄城县| 长兴县| 嵩明县| 东阳市| 连云港市| 昭平县| 元谋县| 德令哈市| 福鼎市| 玉山县| 平塘县| 兴国县| 福海县| 四子王旗| 都安| 神池县| 宜丰县|