0

我正在尝试在表单上预先输入关键字,并且我正在查看响应标头中的结果,它们正在返回整个表而不是返回缩小的结果 - 这导致它(我认为)之前超时返回所有结果。我正在使用从这两个站点找到的带有 ajax 信息的预输入:1)http://www.webmaster-source.com和 2)tatiyants.com

这是 jQuery/javascript 部分

$('#keyword').typeahead({
            minLength: 1,
            source: function (query, process) {
                items = [];
                map = {};
                $.ajax({
                    type: 'post',
                    url: 'includes/php/get_info.php',
                    data: { type: 'keyword', q: query },
                    cache: false,
                    dataType: 'json',
                    success: function (data) {
                        $.each(data, function (i, product) {
                            map[product.keyword] = product;
                            items.push(product.keyword);
                        });
                        return process(items);
                    }
                });
            },
            updater: function (item) {
                if (jQuery.type(map[item]) !== 'undefined'){
                    //add previously used tag
                    $('#keywords').append('<div class="remove" item="'+item+'"><span class="tag">'+item+'</span><button type="button" class="close" item="'+item+'">×</button><input type="hidden" name="ci_keywords[]" value="'+item+'"></div>');
                } else {
                    //add the new tag
                    $('#keywords').append('<div class="remove" item="'+item+'"><span class="tag">'+item+'</span><button type="button" class="close" item="'+item+'">×</button><input type="hidden" name="ci_keywords[]" value="'+item+'"></div>');
                }
            },
            matcher: function (item) {
                if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
                    return true;
                }
            },
            sorter: function (items) {
                items.unshift(this.query);
                return items;
            }
        });

这是我的 get_info.php

if(isset($_POST['type']) && $_POST['type'] == 'keyword') {
    $keywords = $dbh->prepare("SELECT DISTINCT `keyword` FROM `ci_keywords` WHERE `keyword` LIKE ?");
    $keywords->execute(array('%'.$_POST['query'].'%'));
    $results = $keywords->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($results);
}

我页面上的 html 是:

<div class="control-group">
                            <label class="control-label" for="keyword">Keywords or Phrases</label>
                            <div class="controls">
                                <input class="span4" type="text" id="keyword" placeholder="Keywords" autocomplete="off">
                                <div id="keywords" class="clearfix"></div>
                            </div>
                        </div>

所以我的问题 - 为什么它会返回整个列表,而不是通过我发送的搜索词来缩小范围?

4

2 回答 2

3

您发送的变量名称是q

data: { type: 'keyword', q: query },

您在 PHP 中检索的变量名称是query

$keywords->execute(array('%'.$_POST['query'].'%'));

打开错误报告将帮助您避免此问题。

于 2013-07-27T15:12:58.073 回答
1

问题在于用 PHP 构建查询。您将搜索词发送为q但以query. 所以更新$_POST['query']$_POST['q']

于 2013-07-27T15:20:47.757 回答