JQuery Autocomplete, populate with data from pHp json

前端 未结 4 660
鱼传尺愫
鱼传尺愫 2021-01-07 09:39

I am returning a JSON encoded array: echo(json_encode($data)); from php and I would like it to populate the suggest box from JQuery autocomplete. I\'m using thi

4条回答
  •  情书的邮戳
    2021-01-07 10:42

    Try using 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;
            });
        }
    });
    

    JSON Response Example in SearchTest.php

    
    

    Demo Fiddle

    Remote JSONP Demo

提交回复
热议问题