0

索引.php

<html>
<head>
    <title>ы</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
</head>
<body>
<script type="text/javascript">
    $(document).ready(function(){
        $("#search").keyup(function(){
          $("#search").autocomplete("json.php", {
            delay:10,
            minChars:4,
            maxItemsToShow:3
            }
          );
          }); });
      //?word=" + $("#search-text").val()
</script>
<form action=""><input type="search" id="search"> <input type="submit"></form>
<body>
</html>

json.php

<?php
echo json_decode($_GET['q']);

如何使此代码工作?如何使用文件 json?在 php 中如何处理请求并将结果返回给自动完成功能?也许有更好的解决方案?官方文档不包含服务端代码示例!

看起来反应200ok。没有人。回声 json_decode ($ _GET ['q']); 错误在这里,问题是在哪里?

4

3 回答 3

0

为什么要使用 php 进行自动完成?使用js是否比每次击键后重复请求更方便?

任何方式你都可以试试这个

<script>
    $(function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });
    </script>

并在体内

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>


</body>
于 2012-11-15T15:20:19.980 回答
0

您可以使用这些方法来填充您的 items 数组,然后检查字段中的值是否与其中任何一个匹配。

$q = strtolower($_GET["q"]);
if (!$q) return;

/* Connect to database */

/* Query Database */

$items = array();

/* Loop through results and add them to items array */

while ($row = mysqli_fetch_assoc($result)) {
    $items[$row['name']] = $row['value'];
}

foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        echo "$key|$value\n";
    }
}

搜索代码(没有数据库查询位)记录在http://jquery.bassistance.de/autocomplete/demo/

这当然会在每次有人在输入字段中键入字母时生成数组,因此非常耗费服务器。

如上所述,一种更好的方法是使用 availableTags 变量并在加载时通​​过 PHP 将它们加载到 javascript 变量中。我建议为此使用 PHP json_encode 函数。

于 2012-11-16T08:46:33.473 回答
0
$(function() {
    $( "#city" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://sitetesting.kiev.ua/ajax.php",
                dataType: "jsonp",
                data: {
                    name_startsWith: request.term
                },
                success: function( data ) {
                    response( $.map( data.geonames, function( item ) {
                        return {
                            label: item.countryName
                        }
                    }));
                }
            });
        },
        minLength: 2
    });
});
于 2012-11-16T19:33:57.660 回答