LiveSearch PHP和Ajax [英] Livesearch php and ajax
本文介绍了LiveSearch PHP和Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码遇到了一些麻烦,我希望它在每次输入内容时都能执行php文件,但它无法正常工作
I'm having some troubles with my code, I want it to execute the php file whenever I enter something but it isn't working
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function getStates(value) {
$.post("search.php", {name:value},function(data)
$("#results").html(data);
});
}
</script>
</head>
<input type="text" onkeyup="getStates(this.value)"/>
<br>
<div id="results"></div>
<body>
</body>
</html>
php
<?php
mysqli_connect("localhost", "#", "#") or die(mysqli_connect_errno());
mysql_select_db("#") or die(mysql_error());
$search = $_POST["name"];
$players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");
while($player = mysql_fetch_array($players)) {
echo "<div>" . $players["firstname"] . "</div>";
}
?>
推荐答案
据我所知,您应该对此进行更改
From what I can see,you should change this
'%search%'
到
'%{$search}%'
在
$players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");
编辑
@ user3187651假设您已经在服务器端完成了所有操作. 将您的JavaScript更改为:
@user3187651 Assuming you've done everything right on the server side. Change your javascript to:
function getStates(value) {
$.post("search.php", {name:value},function(data){
$("#results").html(data);
}
);
}
这应该消除客户端的错误.
This should get rid of the error in the client side.
这篇关于LiveSearch PHP和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文