我的第一个 stackoverflow 帖子!所以我无法弄清楚为什么这不起作用。以前可以,现在不行,不知道有什么变化。JSON 正确返回,只是没有填充下拉列表。这是html:
$(document).ready(function(){
$('#search').autocomplete({
source: 'search.php',
minLength: 2
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="search">Search</label>
<input type="text" id="search" />
</div>
</body>
这是PHP:
mysql_select_db('symfony',$con);
$autocomplete_value = mysql_real_escape_string($_GET["term"]);
$sql = "SELECT name FROM Artist WHERE name LIKE '%$autocomplete_value%' UNION
SELECT name FROM Event WHERE name LIKE '%$autocomplete_value%'";
$query = mysql_query($sql);
echo $sql;
$results = array();
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
array_push($results, array( 'name' => $row['name']) );
}
json_encode($results);
?>
编辑** 我的同事帮我弄清楚了。我需要改变
array_push($results, array( 'name' => $row['name']) );
至
array_push($results, array( 'value' => $row['name']) );
现在它起作用了!