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

Link to comment
Share on other sites

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.

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.