0

I have a problem, I would like to get a result from a dropdown in php, to fill an other one and create a cascaded dropdown. In the first dropdown, I have categories, and when I select one, I want the second dorpdown to fill automatically with the sub-categories. Here is my code:

$Categorie = new Categorie();
if ($branche_transforme == 1) {
    $liste_categorie = $Categorie->getCategoriesForCdcTransforme();
}
else {
    $liste_categorie = $Categorie->getCategoriesForCdc($cdc_id);
}

echo '<div class="body">';
    echo '<div class="alert alert-info"><span class="glyphicon glyphicon-info-sign"></span>
    Présentation de votre produit telle qu\'elle apparaîtra sur le site Sud de France.</div>';
    echo '<form action="'.$action_form.'" method="POST" id="ajout_produit" enctype="multipart/form-data" >';
    echo '<input type="hidden" name="next_etape" value="etape_cdc" />';
    if (isset($produit_id) && !empty($produit_id) && $produit_id != 0) {
        echo '<input type="hidden" name="produit_id" value="'.$produit_id.'" />';
    }
    echo '<input type="hidden" name="cdc_id" value="'.$cdc_id.'" />';
    echo '<input type="hidden" name="branche_transforme" value="'.$branche_transforme.'" />';
    echo '<table class="formul">';
        echo '<tr class="champ_obligatoire"><th>Nom du produit<span class="etoile">*</span>
                <span title="Pour les vins, précisez le nom de cuvée et le millésime." class="tooltip_span"><img src="img/info.png" alt="" /></span></th>
            <td class="bs-example tooltip-demo"><input type="text" id="nom" name="nom" value="'.$nom.'"
                onblur="verifVide(this);" class="form-control"
                placeholder="Entrez le nom du produit" data-original-title=""
                data-toggle="tooltip" data-placement="right" /></td></tr>';
        echo '<tr class="champ_obligatoire"><th>Catégorie du produit<span class="etoile">*</span></th>';
        echo '<td class="bs-example tooltip-demo"><select name="cat_parent"
                onblur="verifVide(this);" class="form-control"
                data-original-title="" data-toggle="tooltip" data-placement="right">';
        $parent = "";
        foreach($liste_categorie as $categorie) {
            if ($parent != $categorie->getParentNom()) {
                $parent= $categorie->getParentNom();
                $value= $categorie->getParent_id();
                echo '<option value="'.$value.'" >'.$parent.'</option>';
            }
            return $parent;
        }
        echo '</select></td></tr>';
        echo '<th><td class="bs-example tooltip-demo"><select name="categorie_id"
                onblur="verifVide(this);" class="form-control"
                data-original-title="" data-toggle="tooltip" data-placement="right"></th>';

Here you have the first dropdown:

echo '<tr class="champ_obligatoire"><th>Catégorie du produit<span class="etoile">*</span></th>';
        echo '<td class="bs-example tooltip-demo"><select name="cat_parent"
                onblur="verifVide(this);" class="form-control"
                data-original-title="" data-toggle="tooltip" data-placement="right">';
        $parent = "";
        foreach($liste_categorie as $categorie) {
            if ($parent != $categorie->getParentNom()) {
                $parent= $categorie->getParentNom();
                $value= $categorie->getParent_id();
                echo '<option value="'.$value.'" >'.$parent.'</option>';
            }
            return $parent;
        }

And here is the one I want to fill with the result of the previous.

echo '</select></td></tr>';
        echo '<th><td class="bs-example tooltip-demo"><select name="categorie_id"
                onblur="verifVide(this);" class="form-control"
                data-original-title="" data-toggle="tooltip" data-placement="right"></th>';

Honnestly, I don't know how to do. Thank you.

  • you're looking for a cascading drop down, and you'll want to use JQuery to trigger AJAX to populate the data for your second drop down. Here's an example: http://stackoverflow.com/questions/6857287/how-to-make-a-cascading-drop-down-list-in-php-using-jquery – devlin carnate Oct 08 '15 at 15:04
  • @devlincarnate Why does he want to use JQuery for that? – Erwin Moller Oct 08 '15 at 15:08
  • JQuery (or Javascript) will trigger the Ajax call (on change event for the first drop down) – devlin carnate Oct 08 '15 at 15:09
  • When the user select his choice on the first dropdown, I would like to refresh the page and get i back with the category the user selected. And the i need to show the categories that depend of the first dropdown – WuzUrDaddy Oct 08 '15 at 15:33

0 Answers0