0

Hello all

im am after a script that allows for a drop down menu that allows the user to select a item and store that item into the database currenty i have

//all database stuff here

$player_chr_pet = $_POST['chr_pet'];
$chr_health = '10'; // var set at 10 incase error and user cannot select what pet

// if form is submited insert data to database code here 

$html_form = "
<select name=\"chr_pet\">
<option value=\"Pig\">Pig</option>
<option value=\"Dragon\">Dragon</option>
<option value=\"Cow\">Cow</option>
<option value=\"Horse\">Horse</option>
<option value=\"Bear\">Bear</option>
<option value=\"Cat\">Cat</option>
<option value=\"Zebra\">Zebra</option>
<option value=\"Tigar\">Tigar</option>
<option value=\"Chicken\">Chicken</option>
<option value=\"Elephant\">Elephant</option>
</select> 
";

Now i need to know how to set variables according to what the user selected as there option

// if option chicken was selected set the variable health to 20
if ($_POST['chr_pet'] == chicken ) //unsure what goes here instead of chicken 
{
$chr_health = '20';
}
if ($_POST['chr_pet'] == dragon)
{
$chr_health = '50'; 
}
// etc...
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by smantscheff
0

You can store the value with the specific key. Like the example:

$health = array('chicken'=>20,'dragon'=>50,...);

Then parse the value due to the options

$chr_health = $health[$_POST['chr_pet']];

Hope this help.

0

You can have it also the easy way and code the values into you HTML. Then you can store them without any conversion:

<select name="chr_pet">
<option value="10">Pig</option>
<option value="20">Dragon</option>
<option value="30">Cow</option>
<option value="40">Horse</option>
<option value="50">Bear</option>
</select>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.