简体   繁体   English

jQuery AutoSuggest无法正常工作

[英]Jquery autosuggest not working

Hey guys, I'm using jQuery's autosuggest plugin by using php to get the data. 大家好,我通过使用php获取数据来使用jQuery的autosuggest插件。 But it doesn't seem to work since I always get: No Results Found, even though I'm sure there are results: 但这似乎不起作用,因为我总是得到:即使我确定有结果,也找不到结果:

Here's the php code: 这是php代码:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$input = mysql_escape_string($_GET["q"]);
$data = array();
$mysql=mysql_connect('localhost','***','***');
mysql_select_db('jmtdy');
$query = mysql_query("SELECT * FROM users WHERE username LIKE '%".$input."%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>

And the script: 和脚本:

<script >
$(document).ready(function () {

$("#suggestedfriend").autoSuggest("suggestedf.php");
});





</script>
<script >  
  $(document).ready(function () {  
      $("#suggestedfriend").autoSuggest(
           "suggestedf.php",
           {
                selectedValuesProp: "value",
                selectedItemProp: "name",
                searchObjProps: "name"
           });
  });
</script>

Add the above parameters, it will start to work :) 添加以上参数,它将开始工作:)

Just look at data, that server sends you back. 只需查看数据,该服务器就会将您发送回去。 If you use firefox you can watch it in network tab of firebug, or if you use chrome see it in resources. 如果您使用firefox,则可以在firebug的网络标签中观看它,或者如果您使用chrome,则可以在资源中查看它。

The header must be in top of the file, right after the 标头必须位于文件顶部,紧接在

<?php
header('Content-type: application/json');

include_once 'resources/dbconn.php';

$term = $_REQUEST['term'];
$query = "SELECT * FROM cds WHERE titel LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while ($obj = $result->fetch_array()) {
    $arr[] = $obj;
}

//for jsonp echo '('.json_encode($arr).')';
echo json_encode($arr);
?>

The JS/jQuery string JS / jQuery字符串

<script type="text/javascript">

    $(function() {
        var cache = {},
            lastXhr;
        $("#exercise").autocomplete({
            minLength: 2,
            source: function(request, response) {
                var term = request.term;
                if (term in cache) {
                    response(cache[term]);
                    return;
                }
                lastXhr = $.getJSON("json_Search.php", request, function(data,status,xhr) {
                    cache[term] = data;
                    if (xhr === lastXhr) {
                        response(data);
                    }
                });
            }
        });
    });
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM