in another words, i have an drop-down list:
<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
what i want to do is to echo out Backgammon
or Chess
based on their values and on witch one is selected. here is what i have so far. but i get numbers instead of names
$values = $_POST['gamelist'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit']) && ($_POST['submit'] == 'Submit')) {
echo $values;
}
}
thanks
In $_POST['gamelist']
you should have '1' or '2'.
To display it, you should use something similar to the following:
$options = array(
'1' => 'Backgammon',
'2' => 'Chess',
);
$value = $_POST['gamelist'];
echo $options[$value]; // will echo proper option, assuming NO multiselect
This should definitely work.
FYI: The part contained within the <option>
tag is not being passed with form data, only the values assigned within value
attributes to chosen options.
You get numbers because you're using numbers on the value for the options.
<select name="gamelist" id="gamelist">
<option value="Backgammon">Backgammon</option>
<option value="Chess">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
On submit only the value is sent, not the text. If this drop down is coming from the database, you can use the DB to get the title against the ID. If this is a fixed list, then use an array like
$list = array ( 1 = > 'Backgammon' , 2 => 'Chess');
$title = $list[$_POST['gamelist']];
This is just an example, adjust it as per your situation.