What I want to do:
Have one drop-down box that has a list of teams. When a team is selected, I want the second drop-down box to be autopopulated with a list of the team's players.
The problem:
Hmmmmm... I'm not too experienced in Java, so I cannot tell if the problem is PHP or Java.
Here's my code:
<?php
//connect to the database
$link = mysql_connect($host, $user, $passwd);
mysql_select_db($db, $link);
function createTeamOptions(){
$result = mysql_query("SELECT name FROM team ORDER BY name");
$firstFlag = TRUE;
while ($team = mysql_fetch_object($result)){
print " <option value=\"$team->name\"";
if($firstFlag){
print " selected";
$firstFlag = FALSE;
}
print ">$team->name</option>\n";
}
mysql_free_result($result);
}
function createPlayerOption_java(){
$resultTeam = mysql_query("SELECT name FROM team ORDER BY name");
$firstTeamFlag = TRUE;
while($team = mysql_fetch_object($resultTeam)){
//Print the teams selection
if($firstTeamFlag){
print " if(team == \"$team->name\"){\n";
$firstTeamFlag = FALSE;
}
else
print " elseif(team == \"$team->name\"){\n";
//print the players in the teams
$resultPlayer = mysql_query("SELECT * FROM player WHERE teamFFL = \"$team->name\" ORDER BY name");
printf(" toBox.length = %d;\n", mysql_num_rows($resultPlayer));
while($player = mysql_fetch_object($resultPlayer){
print " toBox.options[i++] = Option(\"$player->name\", $player->PrimaryKey);\n";
}
print " }\n";
}
mysql_free_result($resultTeam);
mysql_free_result($resultPlayer);
}
?>
...
<script>
function reflectSelectBox(fromBox, toBox){
team = fromBox.options[fromBox.selectedIndex].text;
i = 0;
<?php createPlayerOption_java(); ?>
if(toBox.length > 1)
toBox.options[0].selected = true;
}
</script>
...
<?php print "<form action=\"$PHP_SELF\" method=get enctype=\"application/x-www-form-urlencoded\">"; ?>
<select name="team" onChange="reflectSelectBox(this.form.team, this.form.player);">
<?php createTeamOptions(); ?>
</select>
<select name="player">
</select>
<input type=submit>
</form>
<?php
//close database connection
mysql_close ($link);
?>
What I've discovered so far:
In the Java function 'reflectSelectBox', if I make any assignments (or anything) dealing with 'toBox', then the function never runs. Inversely, if I comment those lines out, then I can do everything else I need to do.
What I'll get pissed about:
If I misspelled something and it's screwing everything up.