[英]JQuery autocomplete returning data array with label and value, but not showing labels as suggestions
[英]jquery autocomplete not returning data
jQuery自動完成功能讓我頭疼。 鍵入textbox后,自動補全代碼不返回數據。 我似乎可以找到問題所在的代碼。
$().ready(function() {
$("#msg_to").autocomplete({
source:"new_temp.php",
width: 260,
matchContains: true,
selectFirst: false
});
});
我的PHP代碼是:
require('mysql_connect.php');
$word = $_REQUEST['term'];
$s_query = "
SELECT user_id , full_names , userName FROM elib_users
WHERE full_names LIKE '%".$word."%' || userName LIKE '%".$word."%' limit 1";
$sql = mysql_query($s_query) or die(mysql_error());
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
$to_name = $row['full_names'];
$to_id = $row['user_id'];
$to_usName = $row['userName'];
$data[] = array('label' => $to_name = $row['full_names']);
}
}
echo json_encode($data);
HTML文本輸入
<input type="text" name="msg_to" class="btn" id="msg_to" />
使用Firebug調試錯誤( http://getfirebug.com/ )
這對我有用
header('content-type: application/json; charset=utf-8');<br>
$arr = array(
array('id' => 3453, 'label' => 'Producto1', 'value' => 'Producto1'),
array('id' => 3454, 'label' => 'Producto2', 'value' => 'Producto2'),
array('id' => 3455, 'label' => 'Producto3', 'value' => 'Producto3')
);
echo json_encode($arr);
<?php header('content-type: application/json; charset=utf-8');
$arr = array();
$con = mysql_connect("localhost","root","root");
if (!$con) {
die('Todo mal.....: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT id, nombre FROM productos where nombre like '%".$_GET["term"]."%'");
while($row = mysql_fetch_array($result))
{
array_push($arr, array('id' => $row['id'], 'label' => $row['nombre'], 'value' => $row['nombre']));
}
mysql_close($con);
echo json_encode($arr);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.