Demont Posted May 18, 2010 Share Posted May 18, 2010 I'm working on a project, a game, and right now my main issue, though minor is annoying none the less. Users have a list of monsters to choose from, in a drop down box which is populated by the database and ordered by monster level. print "<form action=\"killmonster.php\" method=\"post\">"; print "<select name=\"monstername\" length=\"20\">"; $command3 = "SELECT * from ext_monsters order by lvl asc"; $query3 = mysql_query($command3) or die($db_error); while ($monster = mysql_fetch_array($query3)) { if ($_POST['monstername'] == $monster['name']) { print "<option value=\"".$monster['name']."\" selected=\"selected\">".$monster['name']."</option>"; } else { print "<option value=\"".$monster['name']."\">".$monster['name']."</option>"; } } This is my form for selecting a monster. Now my question is, how do I get the form to hold onto the last option(Monster) that the user chose, so that they don't have to go back to the drop down and find the monster again? What I have now, should work, but it doesn't. The if() statement is just bypassed, and I can't figure out why. Resulting PHP after submit: <center><table width="512" cellpadding="5px" align="center" border="1" style="border-style:dashed; border-width:thin; border-collapse:collapse;" cellspacing="0px"> <tr><td> <center></td> </center><td>Inventory</tr><tr><td> <center>Select the monster you would like to attack:<br /><br> <form action="killmonster.php" method="post"><select name="monstername" length="20"> <option value="Rabbit">Rabbit</option> <option value="Stone Rabbit">Stone Rabbit</option> <option value="Snake">Snake</option> <option value="Cat">Cat</option> <option value="Kobold">Kobold</option> <option value="Brekek">Brekek</option> <option value="Wollo">Wollo</option> <option value="Fae">Fae</option> <option value="Mad Hatter">Mad Hatter</option> <option value="Phoenix">Phoenix</option> <option value="Ho-Ho">Ho-Ho</option> <option value="Fairy Guard">Fairy Guard</option> <option value="Fairy Captain">Fairy Captain</option> <option value="Tree Spirit">Tree Spirit</option> <option value="Tree Spirit Lord">Tree Spirit Lord</option> <option value="Daiku">Daiku</option> <option value="Banshee">Banshee</option> <option value="Medusa Soldier">Medusa Soldier</option> <option value="Medusa">Medusa</option></select> <br><br /> <input type="hidden" name="monsterhp" value="0" /> <input type="submit" name="submit" value="Attack Monster!"><br /><br /> <select name="spell" length="20"> <option value="0">(Select Skill To Cast)</option> <input type="submit" name="cast" value="Cast Spell!"></form></td> <center><td><select name="gemname" width="20" length="20"></select></center></td></tr><br /> </table> </center> Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/ Share on other sites More sharing options...
teamatomic Posted May 19, 2010 Share Posted May 19, 2010 Put this so it will be at the start of your select building block so it will be your first option tag. if$monster!='') { echo "<option value=\"$monster\" selected>$monster</option>"; } You could optionally check $monster against all monsters and just add the "selected" attribute during the build. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060332 Share on other sites More sharing options...
Demont Posted May 19, 2010 Author Share Posted May 19, 2010 That didn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060348 Share on other sites More sharing options...
Demont Posted May 19, 2010 Author Share Posted May 19, 2010 Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060648 Share on other sites More sharing options...
siric Posted May 19, 2010 Share Posted May 19, 2010 Post what you have. Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060757 Share on other sites More sharing options...
Pikachu2000 Posted May 19, 2010 Share Posted May 19, 2010 Change it to: selected="selected" instead of just selected. Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060796 Share on other sites More sharing options...
Psycho Posted May 19, 2010 Share Posted May 19, 2010 $query = "SELECT name FROM ext_monsters ORDER BY lvl ASC"; $result = mysql_query($query) or die($db_error); $monsterOptions = array(); while ($monster = mysql_fetch_array($result)) { $selected = ($_POST['monstername'] == $monster['name']) ? ' selected="selected"' : ''; $monsterOptions[] = "<option value=\"{$monster['name']}\"{$selected}>{$monster['name']}</option>"; } print "<form action=\"killmonster.php\" method=\"post\">\n"; print "<select name=\"monstername\" length=\"20\">\n"; print implode("\n", $monsterOptions); print "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1060809 Share on other sites More sharing options...
Demont Posted May 23, 2010 Author Share Posted May 23, 2010 Hmm, that didn't seem to work, it just re-ordered one of the monsters. Quote Link to comment https://forums.phpfreaks.com/topic/202195-retaining-dropdown-values-from-mysql-populated-form/#findComment-1062088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.