0

我有以下代码调用 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);

?>

但是当我输入任何内容时,它总是会返回我查询中的所有内容。有任何想法吗?

4

2 回答 2

0

Pass the value of the input to the script:

$(function() {
        var val = $( "#vendor_name" ).val();
        $( "#vendor_name" ).autocomplete({
        source: 'vendor_names.php?token=' + val,
        minLength: 3
   });
});

Filter the results returned by the script. You will want to do this in a manner that doesn't concatenate the sql to avoid sql injection, but just for an example:

<?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);

?>
于 2013-05-05T20:15:39.093 回答
0

to my knowledge you need to pass the input by the user to vendor_names.php?input=whatever..
and your query must be
select VendorName from Vendor where VendorCancelDate is NULL and VendorName=%whatever%' order by VendorName

于 2013-05-05T20:16:57.757 回答