开发者

jquery autocomplete with json not working

开发者 https://www.devze.com 2023-02-26 20:48 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消