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
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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