jQuery UI WordPress 自动完成 table

jQuery UI Autocomplete for WordPress table

我在 WordPress 中创建了一个名为 'diseases' 的 table,我想通过插件的自动完成输入访问它。 jQuery UI 自动完成 1.11.4 已包含在内。没有错误,但自动完成不起作用。我可以访问数据库,控制台中的响应等于搜索的词。

TABLE

code      text
B15.0     Epidemischer Ikterus mit Coma hepaticum
...       ....

HTML

<input type="text" name="mybox" id="mybox" />

JS

jQuery(function() {
    jQuery('#mybox').autocomplete({

        // add the way to the file with database query

        source : '../wp-content/plugins/termbrowser/controllers/tags.php',
        minLength : 3,

        // what happens when user chooses autocomlete suggestion

        onSelect : function(suggestion) {
            alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
        }
    });
});

PHP

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-load.php');

$query = isset($_GET['term']) ? $_GET['term'] : FALSE;

    global $wpdb;
    $table = $wpdb->prefix . 'diseases';

    $depts = $wpdb->get_results( "select text from $table where text like '" . $query . "%' order by length(text)"  );

    foreach($depts as $row) {
        $data['value'] = $row->text;
        $data['data'] = $row->code;
    }
    $response = array(
        'suggestions' => $data
    );

   echo json_encode ($response);

尝试使用 'term' 而不是 'query' here 你可以看到插件的例子。 请注意,您需要 return 一个 JSON 数组 id label value:

"id"=>$value, "label"=>$key, "value" => strip_tags($key)

没有里面'suggestions'