MikeEller Posted October 12, 2006 Share Posted October 12, 2006 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):<?phpif (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 Quote Link to comment Share on other sites More sharing options...
keithschm Posted October 12, 2006 Share Posted October 12, 2006 <SELECT NAME="Subject"><OPTION SELECTED>List Item 1</OPTION><OPTION>List Item 2</OPTION><OPTION>List Item 3</OPTION> </SELECT> Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted October 12, 2006 Share Posted October 12, 2006 Wrong: $category = $_POST('category');Right: $category = $_POST['category']; echo $category;Assuming the "newloc" form is what POST'edalso, you need to close your </option> tags and use the value="" attrib Quote Link to comment Share on other sites More sharing options...
MikeEller Posted October 12, 2006 Author Share Posted October 12, 2006 why the echo statement? Quote Link to comment Share on other sites More sharing options...
alpine Posted October 12, 2006 Share Posted October 12, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.