1

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

7
  • did u try to debug $json try echo $json and see in firebug Commented Apr 16, 2011 at 7:19
  • Can you please also post the output of your PHP script? Commented Apr 16, 2011 at 7:19
  • 1
    Also: You should go back to your old questions and select an answer for them by clicking on the check-mark next to the answer that helped most. Otherwise people might no longer help you. Commented Apr 16, 2011 at 7:21
  • It echoes out as follows: ["Texas Rangers","Tampa Bay Rays"] Commented Apr 16, 2011 at 7:21
  • Okay, I didn't know that it was such a big deal... but, i'll do that from now on. Commented Apr 16, 2011 at 7:24

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like you should add the 'value' => ... as well, but hopefully @Lance figured that out.
actually it is not required to provide both label and value, documentation states that if one is missing the other will be used for both purposes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.