jQuery 自动完成从数据库中检索数据
jQuery autocomplete retrieve data from database
我已经为我的网络实现了jQuery自动完成功能,它工作正常。但是我希望自动完成的结果仅检索特定人员的数据,而不是完整的数据库结果。
下面是用于自动完成的 jQuery
jQuery(document).ready(function($){
$('.product_desc').autocomplete({source:'functions/products.php?', minLength:2});
产品.php
//Path for the databsae
<?php
include '../include/configuration.php';
if ( !isset($_REQUEST['term']) )
exit;
$rs = mysql_query('select id,item_name,fk_vendor_id from item where item_name like "%'. mysql_real_escape_string($_REQUEST['term']).'%" order by item_name asc ', $con);
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['item_name'],
'value' => $row['item_name']
);
}
}
else
{
$data[] = array(
'label' => 'not found',
'value' =>''
);
}
// jQuery wants JSON data
echo json_encode($data);
flush();
?>
任何解决方案都将不胜感激。
试试这个:
$(".product_desc").autocomplete({
source: "functions/products.php",
minLength: 2,
select: function(event,ui){
//do something
}
});
试试这段代码,任何具有类.search
自动完成建议的文本字段都将在 ajax 中的服务器端工作.php您需要返回如下数组:
$response = ['Computer', 'Mouse', 'Keyboard', 'Monitor'];
echo json_encode($response);
这是自动建议的示例代码。
$(document).on('keyups','.search',function() {
$(this).autocomplete({
source: function( request, response ) {
if (request.term != "") {
$.ajax({
url: "ajax.php",
dataType: "json",
method: "post",
data: {
name: request.term
},
success: function (data) {
if (data != "") {
response($.map(data, function (item) {
var name = item.name;
return {
label: name,
value: name,
data: item
}
}));
}
}
});
}
},
autoFocus: true,
minLength: 2,
select: function( event, ui ) {
var name = ui.item.data;
$(this).val(name);
}
});
});
相关文章:
- 在一定时间后从SQL数据库检索行
- 尝试显示使用 php 和数据库检索的缩略图中的全尺寸图像
- 从 Web SQL 数据库检索音频并使用 Web 音频 API 播放
- 从数据库检索值时,Jquery mobile复选框未选中
- 使用 JSON 从数据库检索纬度经度到 PHP 页面
- 从数据库检索后在 标记中设置值
- 单击图像时从 MySQL 数据库检索数据
- 从数据库检索用户名的Javascript
- 根据从数据库检索到的值,选中窗体上的单选按钮
- 日期选择器不可用的日期(从数据库检索的数据)
- FullCalendar日期在存储和从数据库检索时不同
- 将从sql数据库检索到的ruby on rails对象存储到javascript对象中
- 使用AJAX从MySQL数据库检索数值数据
- 根据组合框选择从数据库检索数据
- 从数据库检索数据的时间间隔,当数据到达时停止
- 从数据库检索,无需单击使用onfocus的任何按钮
- HTML5SQL使用SELECT语句从web数据库检索记录
- 显示onclick函数后从数据库检索到的tweets
- 从数据库检索存储的复选框值
- 我可以移动数组从PHP到javascript从mysql数据库检索