Lance
Lance

Reputation: 4820

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

Upvotes: 1

Views: 931

Answers (1)

Dalen
Dalen

Reputation: 8996

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

Upvotes: 1

Related Questions