Jump to content

Getting Selected Value from Dropdown box


MikeEller

Recommended Posts

Hello,

I need to be able to get the selected value from a dropdown box on a form.
I have found many ways to dynamically populate the dropdown box, but nothing to get the selected value.

I have something like this (it might be wrong as I am not at home so I cannot see my exact code):

<?php

if (isset($_POST['add'])) {

$category = $_POST('category');

?>

<form name="newloc">
<select name="category">
<option selected>
<option>Item One
<option>Item two
<option>Item Three
</select>

<input type="submit" name="add" value="Add">
</form>

Like I said, there is probably some errors here....but this is the gist of it. I know this is how to get the data from a text box, but I need it to get the value of the selection of the dropdown box.

Any help is greatly appreciated.

Mike
Link to comment
https://forums.phpfreaks.com/topic/23782-getting-selected-value-from-dropdown-box/
Share on other sites

[code]

<?php

$option = $_POST['option']; // value

$options = array("opt1_name"=>"opt1_value","opt2_name"=>"opt2_value");

print "<select name=\"name\">";
foreach($options as $opt_name => $opt_value)
{
  if($opt_value == $option) $selected = "selected=\"selected\">"; else $selected = "";
  print "<option value=\"$opt_value\" $selected>$opt_name</option>";
}
print "</select>";

?>

[/code]

Archived

This topic is now archived and is closed to further replies.

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