Jump to content

Retaining Dropdown values from Mysql populated form?


Demont

Recommended Posts

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>



Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.