开发者

Jquery autosuggest not working

Hey guys, I'm using jQuery's autosuggest plugin by using php to get the data. 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
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_fe开发者_开发技巧tch_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.


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

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜