Dependent selects in PHP

2

I have two selects, the second select being totally dependent on the first. I would like to make the filter, but I can not see a solution. Could someone help me?

<!-- Todas as tecnologias  -->
<div class="mws-form-block">
    <div class="mws-form-row">
        <div class="mws-form-item large">
            <select name="rTecn" onChange="document.formFilter.submit();">
                <option value="">[ Todas as tecnologias ]</option>
                <?php
                    $TECN = getTecnologias();
                    while ($TECNOLOGIA = mysql_fetch_array($TECN)) {
                ?>
                <option value="<?php echo $TECNOLOGIA["TECNOLOGIA"]; ?>" <?php echo ($TECNOLOGIA["TECNOLOGIA"] == $rTecn) ? 'selected' : '' ?>><?php echo $TECNOLOGIA["TECNOLOGIA"]; ?></option>
                <?php
                    }
                ?>
            </select>
        </div>
    </div>
</div>


<!-- Todos os fornecedores  -->
<div class="mws-form-block">
    <div class="mws-form-row">
        <div class="mws-form-item large">
            <select name="rForn" onChange="document.formFilter.submit();">
                <option value="">[ Todos os fornecedores ]</option>
                <?php
                    $conexao->setSQL("SELECT * FROM tab_aro_pcd_riscos, tab_aro_pcd_fornecedores WHERE aro_riscos_mfir = aro_forn_mfir GROUP BY aro_forn_mfir ORDER BY aro_forn_nome ASC");
                    $resultado = $conexao->Consultar();

                    while ($forn = mysql_fetch_array($resultado)) {
                ?>
                <option value="<?php echo $forn["aro_forn_mfir"]; ?>" <?php echo ($forn["aro_forn_mfir"] == $rForn) ? 'selected' : '' ?>><?php echo $forn["aro_forn_mfir"] . " - " . $forn["aro_forn_nome"]; ?></option>
                <?php
                    }
                ?>
            </select>
        </div>
    </div>
</div>
    
asked by anonymous 05.10.2017 / 13:59

1 answer

0

Query by ajax to get the data according to the selected id.

<!-- Todas as tecnologias  -->
<div class="mws-form-block">
    <div class="mws-form-row">
        <div class="mws-form-item large">
            <select name="rTecn">
                <option value="">[ Todas as tecnologias ]</option>
                <?php
                    $TECN = getTecnologias();
                    while ($TECNOLOGIA = mysql_fetch_array($TECN)) {
                ?>
                <option value="<?php echo $TECNOLOGIA["TECNOLOGIA"]; ?>" <?php echo ($TECNOLOGIA["TECNOLOGIA"] == $rTecn) ? 'selected' : '' ?>><?php echo $TECNOLOGIA["TECNOLOGIA"]; ?></option>
                <?php
                    }
                ?>
            </select>
        </div>
    </div>
</div>


<!-- Todos os fornecedores  -->
<div class="mws-form-block">
    <div class="mws-form-row">
        <div class="mws-form-item large">
            <select name="rForn" onChange="document.formFilter.submit();">
            </select>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){

        $('select[name="rTecn"]').on('change', function(){

            var id = $(this).val();

            $.ajax({
                url: 'arquivo.php',
                data: {
                    rTecn: id
                },
                type: 'GET',
                success: function(data){
                    $('select[name="rTecn"]').append(data);
                }
            })

        })

    })
</script>

file.php

<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
$conexao->setSQL("SELECT * FROM tab_aro_pcd_riscos, tab_aro_pcd_fornecedores WHERE aro_riscos_mfir = ".$id." GROUP BY aro_forn_mfir ORDER BY aro_forn_nome ASC");
$resultado = $conexao->Consultar();
$html = '<option value="">[ Todos os fornecedores ]</option>';
while ($forn = mysql_fetch_array($resultado)) {
    $html .= "<option value='".$forn['aro_forn_mfir']."' ".$forn['aro_forn_mfir'] == $rForn ? 'selected' : '' .">".$forn["aro_forn_mfir"] . " - " . $forn["aro_forn_nome"]."</option>";
}
echo $html;
?>
    
24.11.2017 / 17:40