Three dynamic comboboxes

0

I have these 3 dynamic comboboxes:

<fieldset class="grupo">
    <table class="campo" cellspacing="10">
        <tr>

        <td>
        <strong><label for="Valência">Valência</label></strong>
    <select id="Valencia" name="Valencia" style="width:150px">
        <option></option>
        <?php
            $sql = "SELECT DescValencia  FROM centrodb.UtentesDPO GROUP BY DescValencia"; 
            $qr = mysqli_query($conn, $sql);
            while($ln = mysqli_fetch_assoc($qr)){ 
                echo '<option value="'.$ln['DescValencia'].'"> '.$ln['DescValencia'].'</option>'; 
            }
            ?>
    </select>
        </td>
          <td>
    <strong><label for="Denominação">Denominação</label></strong>
    <select id="Descricao" name="Descricao" style="width:150px">
        <option></option>

    </select>
</td>

      <td>
    <strong><label for="Entidade">Entidade</label></strong>
    <select id="Entidade" name="Entidade" style="width:150px">
        <option></option>
    </select>
</td>
     </tr>
 </table>
 </fieldset>

With this javascript:

<script language="javascript">
$('#Valencia').change(function(){ 
        var data = {"valencia":$('#Valencia option:selected').val()};
        $.ajax({
            type:"POST",
            url:"./produto_102",
            dataType:"Json",
            data:data,
            success:function(callback){
                $("#Descricao").html("");
                $.each(callback,function(key,value){
                    $("#Descricao").append(value);
                });
            },
            error:function(callback){
                console.log(callback);
            }
        });
    });
    $('#Descricao').change(function(){ 
        var data = {"Descricao":$('#Descricao option:selected').val()};
        $.ajax({
            type:"POST",
            url:"./produto_102",
            dataType:"Json",
            data:data,
            success:function(callback){
                $("#Entidade").html("");
                $.each(callback,function(key,value){
                    $("#Entidade").append(value);
                });
            },
            error:function(callback){
                console.log(callback);
            }
        });
    });
</script>

and the php code in another file:

if((isset($_POST["valencia"]))&&!empty($_POST["valencia"])){
    $valencia = $_POST["valencia"];
    $sql="SELECT DISTINCT Descricao FROM centrodb.UtentesDPO WHERE DescValencia = '$valencia' ORDER BY Descricao ASC";
    $qr = mysqli_query($conn, $sql);
    while($ln = mysqli_fetch_assoc($qr)){
        $Descricao[]='<option value="'.$ln['Descricao'].'">'.$ln['Descricao'].'</option>';
    }
    echo json_encode($Descricao);
}
if((isset($_POST["Descricao"]))&&!empty($_POST["Descricao"])){
    $Descricao1 = $_POST["Descricao"];
    $sql="SELECT Codigo FROM centrodb.UtentesDPO WHERE Descricao = '$Descricao1' ORDER BY Codigo ASC";
    $qr = mysqli_query($conn, $sql);
    while($ln = mysqli_fetch_assoc($qr)){
        $Entidade[]='<option value="'.$ln['Codigo'].'">'.$ln['Codigo'].'</option>';
    }
    echo json_encode($Entidade);
}

The result of the first two combobox is correct because the second depends on the choice of the first one, but the third combobox only depends on the choice of the second combobox, but I intend that it also depends on the first combobox. Can you help?

    
asked by anonymous 17.08.2018 / 12:37

0 answers