jquery自动完成不返回数据

时间:2013-06-06 19:22:50

标签: php jquery mysql

jquery autocomplete让我头疼。键入文本框后,自动填充代码不会返回数据。我似乎可以在代码中找到问题所在。

  $().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"  />

1 个答案:

答案 0 :(得分:0)

使用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);
?>