php 搜索框提示(自动完成)实例代码

2019-04-08 16:39:21刘景俊

</style>
<title>jquery+php实现用户输入搜索内容时自动提示</title>
</head>

<body>
<div id=”search”>
<input type=”text” name=”k” /> <input type=”button” name=”s” value=”搜索” />
</div>
<div id=”search_auto”></div>
</body>
</html>

<script src=”jQuery.js”></script>
<script>
$(function(){

$(‘#search_auto').css({‘width':$(‘#search input[name="k"]‘).width()+4});
$(‘#search input[name="k"]‘).keyup(function(){
$.post(‘search_auto.php',{‘value':$(this).val()},function(data){
if(data=='0′) $(‘#search_auto').html(”).css(‘display','none');
else $(‘#search_auto').html(data).css(‘display','block');
});
});

});
</script>

  服务器端完整代码:

<?php
$v=$_POST[value];
$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");
if(mysql_num_rows($re)<=0) exit('0');
echo '<ul>';
while($ro=mysql_fetch_array($re)) echo '<li><a href="">'.$ro[title].'</a></li>';
echo '<li><a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a& amp; gt;</li>';
echo '</ul>';
?>

相关文章 大家在看