Jump to content

Archived

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

MikeEller

Getting Selected Value from Dropdown box

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

Share this post


Link to post
Share on other sites
<SELECT NAME="Subject">
<OPTION SELECTED>List Item 1</OPTION>
<OPTION>List Item 2</OPTION>
<OPTION>List Item 3</OPTION>
</SELECT>

Share this post


Link to post
Share on other sites
Wrong: $category = $_POST('category');

Right: $category = $_POST['category']; echo $category;

Assuming the "newloc" form is what POST'ed

also, you need to close your </option> tags and use the value="" attrib

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.