Problem with Select and PHP

Asked

Viewed 38 times

0

I’m making a form where the person selects the Item 1, when selecting it another select appears with the categories of this item 1. The problem is when it has more than 1 item, as it is taking the select of the last category envés to pick the selected category. I tried some fixes but none worked.

This is the SELECT code:

<?php $procura_cursos = $site->query("SELECT * FROM cursos");
if ($procura_cursos->rowCount() <=0) {

}else{
    while ($curso = $procura_cursos->fetch(PDO::FETCH_OBJ)) {
        $procura_tumas = $site->query("SELECT * FROM turmas WHERE curso = '$curso->chns' AND inscricoes ='ativo'");
        if ($procura_tumas->rowCount() <=0) {?>
            <div class="col-md-6 selection tipo<?echo$curso->chns;?>" style="display: none;">
                <label>Sem Horário Disponível para o Curso</label>
            </div>
            <?
        }else{?>
            <div class="col-md-6 selection tipo<?echo$curso->chns;?>" style="display: none;">
                <label for="turma">Escolha os Horários Disponíveis</label>
                <select class="seleciona form-control" name="turma">
                    <option disabled>Selecione o Dia</option>
                    <?while ($turmas = $procura_tumas->fetch(PDO::FETCH_OBJ)) {
                        ?>
                        <option value="<?php echo$turmas->chns; ?>"><?php echo $turmas->turma; ?></option>
                        <?
                    }?>
                </select>
            </div>
            <?
        }
    }
} 
?>

This is the SCRIPT code:

<?php $procura_cursos = $site->query("SELECT * FROM cursos");
if ($procura_cursos->rowCount() <=0) {

}else{
    while ($curso = $procura_cursos->fetch(PDO::FETCH_OBJ)) {
        $procura_tumas = $site->query("SELECT * FROM turmas WHERE curso = '$curso->chns' AND inscricoes ='ativo'");
        if ($procura_tumas->rowCount() <=0) {?>

            <?
        }else{?>
            <script>
                $("select#curso").change(function() {
                    <?while ($turmas = $procura_tumas->fetch(PDO::FETCH_OBJ)) {
                        ?>
                        if ($(this).val() == "<?echo$curso->chns;?>") {
                            $(".tipo<?echo$curso->chns;?>").css("display", "block").fadeIn();
                            <?php 
                            $procura_cursos0 = $site->query("SELECT * FROM cursos where chns != '$curso->chns'");
                            if ($procura_cursos0->rowCount() <=0) {

                            }else{
                                while ($curso0 = $procura_cursos0->fetch(PDO::FETCH_OBJ)) {
                                    $procura_tumas0 = $site->query("SELECT * FROM turmas WHERE curso != '$curso0->chns' AND inscricoes ='ativo'");
                                    if ($procura_tumas0->rowCount() <=0) {

                                    }else{
                                        while ($turmas0 = $procura_tumas0->fetch(PDO::FETCH_OBJ)) {?>
                                            $(".tipo<?echo$curso0->chns;?>").css("display", "none").fadeOut();
                                            <?  
                                        }
                                    }
                                }
                            }
                            ?>
                        }
                        <?
                    }?>
                });
            </script>
            <?
        }
    }
} ?>

1 answer

0

On the onchange you have to pick up the id that was selected.

var id = $('#id_do_select'). val();

and from then on you will treat according to the id that was selected.

Browser other questions tagged

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