0

フォームのキーワードの先行入力を試みていますが、応答ヘッダーの結果を見ていて、絞り込まれた結果を返すのではなく、テーブル全体を返しています。すべての結果を返します。次の 2 つのサイトから見つけた 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 に答える