我是jQuery的新手。我正在尝试jQuery自动完成远程数据源这里是我的代码:
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Quanta Plus">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body>
<meta charset="utf-8">
<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
}
</style>
<script>
$(function() {
var cache = {},
lastXhr;
$( "#birds" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
cache[ term ] = data;
if ( xhr === lastXhr ) {
response( data );
}
});
}
});
});
</script>
<div class="demo">
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" value="" />
</div>
</div><!-- End demo -->
</body>
</html>
文件:search.php
<?php
require_once("includes/connection.php");
$q = strtolower($_GET["birds"]);
echo $q;
$return = array();
$query = "SELECT title
FROM `project`
WHERE `title` LIKE CONVERT( _utf8 '%$q%'
USING latin1 )";
$resultset=mysql_query($query,$connection);
$json = '[';
$first = true;
while ($row = mysql_fetch_assoc($resultset)) {
if (!$first) {
$json .= ',';
} else {
$first = false;
}
$json .= '{"value":"'.$row['title'].'"}';
}
$json .= ']';
echo $json;
?>
收到以下错误:
Notice:
Undefined
index: birds in /var/www/html/workbench/sudeepc/project/search.php
on line 4
[
{"value":"chandrayaan"},
{"value":"Project Management System"},
{"value":"shoping cart"},
{"value":"hospital management system"}
]
fire bug在搜索该文本框时显示以下错误,并且没有搜索建议。
如何解决此问题?
答案 0 :(得分:2)
根据评论,似乎第一个错误是由于查找错误的$ _GET变量引起的。使用Firebug发现并正确纠正它的道具。
当前没有填充值的原因是因为在JSON前面回显了额外的字符串。删除行echo $q;
,它应该有效。