简体   繁体   中英

jquery autocomplete not returning data

jquery autocomplete giving me a headache. The autocomple code is not returning data after typing into textbox . I can seem to find where the issue is in the code.

  $().ready(function() {
$("#msg_to").autocomplete({
    source:"new_temp.php",
    width: 260,
    matchContains: true,
    selectFirst: false
});
});

My php code is:

 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 text input

<input type="text" name="msg_to" class="btn" id="msg_to"  />

Use Firebug to debug you error ( http://getfirebug.com/ )

This works for me

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM