Miko Posted September 24, 2009 Share Posted September 24, 2009 Hello, I have here a select menu and in that I have several optgroups with several options (php loop that prints out the values). What I want to do is getting 2 values from the select menu. I got this code: <select name="reason" onChange="getReason(this.value)"> <option selected>Select a request</option> <?php $sql2 = "SELECT * FROM tbl_group_reason"; $query2 = mysql_query($sql2); while($row2 = mysql_fetch_array($query2)){ $group_id = $row2['ID']; $group_reason = $row2['GROUP_REASON']; echo "<optgroup label=\"$group_reason\" name=\"group_reason\" value=\"$group_id\">"; $sql3 = "SELECT * FROM tbl_reason WHERE GROUP_ID = '$group_id' "; $query3 = mysql_query($sql3); while($row3 = mysql_fetch_array($query3)){ $reason = $row3['REASON']; $reason_id = $row3['REASON_ID']; echo "<option value=\"$reason_id\">".$reason."</option>"; } echo "</optgroup>"; } ?> </select> Getting the value of the <option> isn't a problem, but getting the value of the <optgroup> that I don't know how to do. Anyone? Thanks! Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Hi Miko, I think you are going to have to change the name of the SELECT to something like reason[] which will make the optgroup results store in an array. Then, do a foreach loop on the POST data to extract the results. i.e.: foreach($_POST['reason'] as $opt) { echo $opt . '<br />'; } I hope this helps. Quote Link to comment Share on other sites More sharing options...
Miko Posted September 24, 2009 Author Share Posted September 24, 2009 Hi Bricktop, thanks for your answer. Will this "keep in mind" that the values of the optgroup and option need to stay "seperated"? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Hmm, not sure on that one Miko, give it a try and see what results you get - I'm sure if it doesn't produce the desired result the code can be tweaked so it does. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2009 Share Posted September 24, 2009 but getting the value of the <optgroup> that I don't know how to do.That's because <optgroup> tags don't have name and value attributes. Only <option> tags have values. Ref: http://w3schools.com/tags/tag_optgroup.asp Whatever you are trying to do, you must do using the value="..." attribute of each <option> tag. 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.