1

我正在返回一个 JSON 编码数组:echo(json_encode($data));来自 php,我希望它从 JQuery 自动完成填充建议框。我正在使用这个:

$("#field").autocomplete({
            source : "SearchTest.php",
            maxLength: 5
        });

不知道为什么这不起作用。每次按键后,我都会检索数据并用该数据填充建议框,我不希望自动完成为我排序和选择,我正在做服务器端。现在它只是一个字符串列表。能够自定义数据的呈现方式也很好。

编辑:将来源更改为发布:

$("#field").autocomplete({
            source : function(request, response) {
                $.post("SearchTest.php", request, response);
            },
            maxLength : 5
        });

现在收到此错误:

Uncaught TypeError: Cannot use 'in' operator to search for '1240' in 
Notice: Undefined index: field in /.../SearchTest.php on line 25

第 25 行是:$whatTheyWantToSearch = $_POST['field'];

4

5 回答 5

4

尝试使用ajax

var searchRequest = null;
$("#field").autocomplete({
    maxLength: 5,
    source: function(request, response) {
        if (searchRequest !== null) {
            searchRequest.abort();
        }
        searchRequest = $.ajax({
            url: 'SearchTest.php',
            method: 'post',
            dataType: "json",
            data: {term: request.term},
            success: function(data) {
                searchRequest = null;
                response($.map(data.items, function(item) {
                    return {
                        value: item.name,
                        label: item.name
                    };
                }));
            }
        }).fail(function() {
            searchRequest = null;
        });
    }
});

SearchTest.php 中的 JSON 响应示例

<?php
header('Content-type: application/json');
echo '{"items":[{"name":"Ashok"},{"name":"Rai"},{"name":"Vinod"}]}';
?>

演示小提琴

远程 JSONP 演示

于 2013-07-20T17:26:10.163 回答
2

来自 php 的正确 json 格式:

<?php
   echo '[ {"name1":"val1"},{"name2":"val2"} ]'; 
?>

来自 js 的意思是 {} 对象的 []-array。

在我的自动完成widjet的情况下,这很好用:

    $response="[";
    while($row = $res->fetch_assoc()){
        if($response !="[")$response.=",";
        $response.='{"label":"'.$row["fio"].'","value":"'.$row["id"].'"}';
    }
    $response.="]";

    echo $response;
于 2015-12-03T08:57:04.943 回答
0

源参数可能有问题。它应该是'/Searchtest.php'吗?

http://api.jqueryui.com/autocomplete/#option-source

于 2013-07-20T17:10:15.527 回答
0
<html>
    <head>
            <title>Ajax</title>
            <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
            <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
            <script>
                 $(function() { 
                    $("#myname").autocomplete({
                      source: 'emp.php',
                      select: function (event, ui) {
                        $("#myname").val(ui.item.label);
                        $("#myid").val(ui.item.id);
                    },
                       minLength: 0,
                       autoFocus: true,
                    }); 
                });
            </script>
    </head>
    <body>
        <h2>Ajax Testing</h2>
        <input  name="myname" id="myname" type="text">
        <input  name="myid" id="myid" type="text">
    </body>
</html>
-------------- Below is the code of PHP for emp.php page --------------------------
<?php
require_once 'connection.php';
$query  = "SELECT myname as label , myid as id  FROM emp WHERE name LIKE ? ORDER BY name";
$rsTable = sqlsrv_query($conn, $query,array('%'.$_REQUEST['term'].'%'));
while ($row_rsTable = sqlsrv_fetch_array($rsTable, SQLSRV_FETCH_ASSOC)) {

    $emparray[] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $row_rsTable);
}
echo json_encode($emparray);
sqlsrv_close($conn);
?>
于 2021-03-10T04:49:38.403 回答
0

像这样的东西是最好的方法。json_encode 为您完成所有工作。

    $result = $_mysqli->query(...);
    $rs = array();
    $pos = 0;
    while($row = $result->fetch_assoc()){
        $rs[$pos]["n1"] = $row["n1"];
        $rs[$pos]["n2"] = $row["n2"];
        ...
        $rs[$pos++]["nn"] = $row["nn"];

    }
    header('Content-type: application/json');
    echo json_encode($rs);
于 2016-03-20T02:50:43.970 回答