Dependent selects in PHP

Asked

Viewed 405 times

1

I have two selects, being the second select totally dependent on the first. I would like to perform the filter, but I’m not able to visualize 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>
  • 1

    What is the error that is happening? I don’t understand. Note: This code is about 10 years old? Register Globals has become OBSOLETE since PHP 5.3.0. Relying on this feature is highly not recommended.

  • I confess to you that the code is very old. I’m actually maintaining it and an update activity has already been required

  • Regarding the error, I would actually like the information of the second select to be reflected based on the choice of the first filter

  • In the second select you must add the value of $rTecn sql Where to bring only suppliers related to the selected technology. There is this relationship between technology and vendor in your database?

1 answer

0

Make a query by ajax to take 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>

php file.

<?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;
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.