简体   繁体   中英

jquery autocomplete with json not working

I have successfully used the autocomplete function with a hard coded array. But, when I try to use data from a php file, it doesn't work.

My jquery is as follows.

<script type="text/javascript">
$(document).ready(function(){
    $("input#game_two_other").autocomplete({
        source: "mlb_other_teams.php",
        minLength: 3
    });
});
</script>

My php code is as follows.

$mister =   mysql_query("SELECT * FROM al_other WHERE user_id = '".$_SESSION['id']."'") or die(mysql_error());

while ($other = mysql_fetch_assoc($mister)) 
{

    $team_one   =   $other['team_one'];
    $team_two   =   $other['team_two'];

}

$json = array($team_one, $team_two);

echo json_encode($json);

Any ideas or thoughts?

Thanks,

Lance

When you produce json for jquery's autocomplete it has to containtains label and/or value properties:

$mister =   mysql_query("SELECT * FROM al_other WHERE user_id = '".$_SESSION['id']."'") or die(mysql_error());

$json = array()

while ($other = mysql_fetch_assoc($mister)) 
{

   $json[] = array('label'=>$other['team_one']);
   $json[] = array('label'=>$other['team_two']);

}

echo json_encode($json);

Similar question: Having problems with jQuery UI Autocomplete

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