我有以下代码调用jquery自动完成小部件:
$(function() {
$( "#vendor_name" ).autocomplete({
source: 'vendor_names.php',
minLength: 3
})
});
我的vendor_names.php
文件类似于:
<?php
include("include/db_connect.php");
$query = "select VendorName from Vendor where VendorCancelDate is NULL order by
VendorName";
$result = mssql_query($query);
while ( $record = mssql_fetch_array($result) ){
$vendors[] = array('label' => $record['VendorName']);
}
echo json_encode($vendors);
?>
但是当我输入任何内容时,它总是返回查询中的所有内容。有什么想法吗?
答案 0 :(得分:0)
将输入值传递给脚本:
$(function() {
var val = $( "#vendor_name" ).val();
$( "#vendor_name" ).autocomplete({
source: 'vendor_names.php?token=' + val,
minLength: 3
});
});
过滤脚本返回的结果。您将希望以不连接sql以避免sql注入的方式执行此操作,但仅作为示例:
<?php
include("include/db_connect.php");
$val = $_GET["token"];
$query = "select VendorName from Vendor where VendorCancelDate is NULL and VendorName like '%". $val . "%' order by VendorName";
$result = mssql_query($query);
while ( $record = mssql_fetch_array($result) ){
$vendors[] = array('label' => $record['VendorName']);
}
echo json_encode($vendors);
?>
答案 1 :(得分:0)
据我所知,您需要将用户的输入传递给vendor_names.php?input=whatever
..
并且您的查询必须是
select VendorName from Vendor where VendorCancelDate is NULL and VendorName=%whatever%' order by VendorName