2011-10-27 111 views
0

这是我的jQuery函数:自动填充返回undefined

$(function() { 
    $("#user").autocomplete({ 
     source: 'spiderman/null/users.php', 
     minLength: 2 
    }); 
}); 

当我开始在我的

<input type="text" class="highlight" id="user" name="user"/> 

是表示说:“不确定的”一个项目列表输入最少2个字母。 似乎它是一个无效的来源,但我敢肯定它是有效的,因为我已经在同一目录中制作了include_once,并且加载了源脚本。

这是我的PHP代码:

if(!$_GET['term']) { 
    die(); 
} 

$return_arr = array(); 
$ac_term = "%".$_GET['term']."%"; 
$query = "SELECT `name` FROM `users` WHERE `name` LIKE :term"; 
$result = $conn->prepare($query); 
$result->bindValue(":term", $ac_term); 
$result->execute(); 

/* Retrieve and store in array the results of the query.*/ 
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { 
    $row_array['name'] = $row['name']; 

    array_push($return_arr, $row_array); 
} 

/* Toss back results as json encoded array. */ 
echo json_encode($return_arr); 

我测试过的PHP代码也和它运作良好:作为长期lol它返回:

[{"name":"LolShit"},{"name":"Lolipop"},{"name":"Lolo"},{"name":"Lolololololololo"},{"name":"Loll"},{"name":"Pro Lol"}] 
+0

您是否需要设置json文件类型标头? – Tim

+0

@Tim你在做什么? – Cyclone

回答

0

我添加

$row_array['value'] = $row['name'];

而且它的工作...该死的。 我希望可以回答自己的问题,哈哈。

0

我想你或者只是想让你的JSON数组看起来像这样:

["LolShit","Lolipop","Lolo","Etc..."] 

或者如果你想使用一个对象p rovide一个labelvalue(其UI自动完成预计):

[{"label":"LolShit","value":"LolShit"},{"label":"Lolipop","value":"Lolipop"},...]