Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax响应引导typeahead不会缩小结果范围_Php_Jquery_Ajax - Fatal编程技术网

Php 使用ajax响应引导typeahead不会缩小结果范围

Php 使用ajax响应引导typeahead不会缩小结果范围,php,jquery,ajax,Php,Jquery,Ajax,我试图为表单上的关键字设置一个typeahead,我查看响应标题中的结果,结果返回整个表,而不是返回缩小的结果-这导致它(我认为)在返回所有结果之前超时。我正在使用从以下两个站点找到的带有ajax信息的typeahead:1)和2) 以下是jQuery/javascript部分 $('#keyword').typeahead({ minLength: 1, source: function (query, process) {

我试图为表单上的关键字设置一个typeahead,我查看响应标题中的结果,结果返回整个表,而不是返回缩小的结果-这导致它(我认为)在返回所有结果之前超时。我正在使用从以下两个站点找到的带有ajax信息的typeahead:1)和2)

以下是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;
            }
        });
我的页面上的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>

关键词或短语

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

您发送的变量名是
q

data: { type: 'keyword', q: query },
在PHP中检索的变量名是
query

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

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

您发送的变量名是
q

data: { type: 'keyword', q: query },
在PHP中检索的变量名是
query

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

启用错误报告将帮助您避免此问题。

问题在于如何用PHP构建查询。您将搜索词作为
q
发送,但作为
query
访问。因此,将
$\u POST['query']
更新为
$\u POST['q']

问题在于如何用PHP构建查询。您将搜索词作为
q
发送,但作为
query
访问。因此,将
$\u POST['query']
更新为
$\u POST['q']

Ranty,这真是太尴尬了!就这样!我真的很感激!兰蒂,这太尴尬了!就这样!我真的很感激!