Jump to content


Photo

Getting Selected Value from Dropdown box


  • Please log in to reply
4 replies to this topic

#1 MikeEller

MikeEller
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 12 October 2006 - 06:47 PM

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

#2 keithschm

keithschm
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 12 October 2006 - 07:05 PM

<SELECT NAME="Subject">
<OPTION SELECTED>List Item 1</OPTION>
<OPTION>List Item 2</OPTION>
<OPTION>List Item 3</OPTION>
</SELECT>
NEED HELP

#3 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 12 October 2006 - 07:06 PM

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
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#4 MikeEller

MikeEller
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 12 October 2006 - 07:08 PM

why the echo statement?

#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 12 October 2006 - 07:23 PM


<?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>";

?>






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users