Jump to content

get value of a optgroup


Miko

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/175326-get-value-of-a-optgroup/
Share on other sites

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

Archived

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

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