0

I have made a dropdown menu where the user selects the kind of product:

$query="SELECT id, name FROM product ORDER BY name";
$result=mysql_query($query);
//  $products.= "<option value=\"\">";
    while($record=mysql_fetch_row($result)){
        $product_id=$record[0];
        $product_name=$record[1];

    if($product_id==$p_product_id){
        $selected="SELECTED";
    }else{
        $selected="";
    }

    $products.= "<option value=\"$product_id\" $selected>$product_name";
}

I am showing the dropmenu as followed:

<tr>
<td             >Product</td>
<td             ><select name=product style=\"width:150px;\">$products</select></td>
</tr>

No problem so far, based on the selection which is made i want to show different options, named categories, i have linked each category to an product ID.

$query="SELECT categorie.id, categorie.name, categorie.product_id, product.id FROM categorie LEFT OUTER JOIN product ON categorie.product_id = product.id WHERE active='1' ORDER BY order, omschrijving";
$result=mysql_query($query);
//  $categories.= "<option value=\"\">";
    while($record=mysql_fetch_row($result)){
    $categorie_id=$record[0];
    $categorie_name=$record[1];
    $categorie_product_id=$record[2];
    $product_id=$record[3];

    if($categorie_id==$p_categorie_id){
        $selected="SELECTED";
    }else{
        $selected="";
    }
if($
        $categorien.= "<option value=\"$categorie_id\" $selected>$categorie_name";
    }

Dropdown:

<td             >Categorie</td>
    <td             ><select name=categorie style=\"width:150px;\">$categorien</select></td>

I am kinda lost how i can edit my code, so i can show the correct category based on the choosen product (ID). Can someone give me tip to start with?

Mark Van
  • 281
  • 4
  • 21
  • use ajax for fetching depended records – urfusion Jan 06 '16 at 21:29
  • ^ or `onchange='formname.submit()'` (in your ` – developerwjk Jan 06 '16 at 21:30
  • This is a demo of something you might be able to add your data varaibles to http://stackoverflow.com/questions/34361452/many-elements-of-a-listbox-to-another/34367418#34367418 - Plonk it into a `.php` page and have a play with it - the key is to use numbers for the values and "look them up" in arrays - also makes keeping things clean easier as you are only submitting interger values and can clean them with `intval();` – Steve Jan 06 '16 at 21:37

0 Answers0