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...

Recommended Answers

All 2 Replies

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.

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.