JQuery从数据库自动完成
JQuery Autocomplete from Database
我需要为我的网站做自动完成建议,数据应该从数据库中检索。我想使用JQuery自动完成。这是我的代码,但它不起作用!这是我的php文件,名为gethint.hp:
<?php
require_once ('config.php');
$q=$_REQUEST["q"];
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
$result = mysql_query($sql);
$json=array();
while($row = mysql_fetch_array($result)) {
$json[]=array(
'value'=> $row['fname'],
'label'=> $row['fname']
);
}
echo json_encode($json);
?>
然后这是我的html文件:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function(){
$("#hint").autocomplete({
source:'gethint.php',
minLength:1
});
});
</script>
</head>
<body>
<form class="sansserif" action="view.php" method="post">
Name: <input type="text" id="hint" name="hint" >
<input type="submit" name="submit" value="View">
</form>
</html>
这花了很多时间,但我找不到问题。我在想是否有人能帮我。谢谢
<?php
require_once ('..'config.php');
$q=$_REQUEST["term"];
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$json[]=array(
'value'=> $row['fname'],
'label'=> $row['fname']
);
}
echo json_encode($json);
?>
我所更改的只是$_REQUEST["q"]到$_REQUEST["term"]。
我做了一些更改,也许你需要修复一些东西,但看看是否有帮助。。。
php:
<?php
require_once ('config.php');
$q=$_REQUEST["q"];
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
$result = mysql_query($sql);
$json=array();
while($row = mysql_fetch_array($result)) {
array_push($json, $row['fname']);
}
echo json_encode($json);
?>
html+jquery:
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
</head>
<body>
<form class="sansserif" action="view.php" method="post">
Name: <input type="text" id="hint" name="hint" />
<input type="submit" name="submit" value="View">
</form>
<script type="text/javascript">
$(function() {
$( "#hint" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "gethint.php",
dataType: "jsonp",
data: {
q: request.term
},
success: function( data ) {
response( data );
}
});
},
});
});
</script>
</body>
</html>
您的PHP脚本应该接受term
参数,而不是q
参数。
相关文章:
- 如何通过php页面将数据库值检索到jquery自动完成框中
- Jquery(多读/少读)在从数据库加载整个数据后不起作用
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 将动态feild数据存储到jquery-mobile和php-mysql中的数据库中
- 使用jQuery/Ajax从数据库中检索记录
- 如何使用JQuery将值数组插入到数据库中
- 使用Ajax和JQuery从SQL数据库中添加和删除一行
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- 尝试使用 ASP-classic 和 jQuery 根据数据库的结果自动选择下拉选项
- 使用javascript、jquery和PHP自动完成从数据库中提取城市的输入
- 如何在使用jquery向数据库发送数据时取消激活的按钮
- 需要帮助-用数据库中的数据替换DIV内容而不刷新(使用-jquery、hmlhttp、CI)
- 使用jquery ajax更新数据库;不起作用
- 每个数据库行的jQuery对话框
- jQuery在没有数据库的情况下从php-json自动完成
- 从数据库检索值时,Jquery mobile复选框未选中
- 使用jquery在googlemapapi中放置多个标记,coords将来自数据库
- 使用jquery验证插件检查数据库中是否存在电子邮件
- 如何使用javascript从数据库中获取值,我使用的是jquery自动完成,我只想从数据库中搜索值
- 切换结果来自数据库jQuery