dongshi1207 2017-09-08 14:21
浏览 61
已采纳

WordPress表的jQuery UI自动完成

I´ve created a table in WordPress called 'diseases' and I want to access to it with a autocomplete input from a plugin. jQuery UI Autocomplete 1.11.4 is already included. There is no error but autocomplete do not work. I get access to the database and the response in the console equal the searched term.

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);
  • 写回答

1条回答 默认 最新

  • duanhuiuw12555 2017-09-08 14:52
    关注

    Try using 'term' instead of 'query' here you can see the example of the plugin. Notice that you need to return a JSON array with id label value:

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

    no inside 'suggestions'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧