Question : PHP MySQL SELECT OPTGROUP parent value

Hi folks, let me see if I can explain this right..  I am somewhat of a newbie to posting so please bear with me...

My Goal:  I want to be able to populate an input field in a form based on my selection from a MySQL populated php SELECT dropdown with optgroup.  I am using Option Groups <optgroup>.   AFTER UPDATE of the SELECT object, I want (OR maybe not) a javascript to update ANOTHER field with the OPTGROUP value.

This is an option group drop down of Asset Types and Models;
Meaning -
Laptop = LatitudeX, LatitudeY, LatitudeZ, etc.
Desktop = OptiPlex GX270, OptiPlex GX280, OptiPlex GX620
Printer = LaserJet 8150, laserJet blahhhhh, etc.

My table structure is like this:
Table #1 - A single table named asset_types with just a single field, primary key - AssetType.
Table #2 - A table named models with 3 fields ID, AssetTypeID (which matches AssetType) and Model.

The attached code works fine and populates and displays just what it needs to do.  (I'm hoping the solution will revolve around existing code) Except..

The Question:  I would like to populate another field with the OPTGROUP value (AssetType) so I can submit it to my DB seperately and search on it.  Please dont suggest any major design changes, I know it could be better but this is what I have to work with.  

Upon selecting a MODEL from my drop down <SELECT> list of models, I want to populate the AssetType <INPUT> field "onChange" with the model's parent.  In this example I would like to select a 3100CN and have the AssetType filed be automatically populated with "Printer".

This is one of those "How do I pass a javascript variable to PHP" questions.
I know I can pass a PHP variable to JS, but the other way is tough!

I tried creating a JS function where I went out to PHP to requery based on my selection but I could not figure out the MY JS VAR HERE part!

function SelectParent(){
  <?
  $q2=mysql_query("select distinct(MyGroup)  from progareas where [MY JS VAR HERE!!!]");
?>
......

Let me know if anyone needs any additional information.

Thanks and best regards to all!





 
 
Tree optgroup structure
308869
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
<select>
<?
$sql_model = "SELECT asset_types.AssetType, models.AssetTypeID, models.Model FROM asset_types INNER JOIN models ON asset_types.AssetType = models.AssetTypeID ORDER BY AssetType, Model";
$result_model = mysql_query($sql_model);
$group_model = array();
while ($row_model = mysql_fetch_assoc($result_model))
{
$group_model[$row_model['AssetType']][] = $row_model;
}
foreach ($group_model as $key => $values)
{
echo '<optgroup label="'.$key.'">';
foreach ($values as $value) 
{
echo '<option value="'.$value['Model'].'"> '.$value['Model'].' </option>';
}
echo '</optgroup>';
}

?>
</select>

Answer : PHP MySQL SELECT OPTGROUP parent value

I got it:


function SelectParent(val,idx){

var sel=document.getElementById('ProgramArea');
alert(val+' : '+sel.options[idx].parentNode.label);

} // end of SelectParent

Random Solutions  
 
programming4us programming4us