Pass jquery variable to search subcategory

Asked

Viewed 87 times

0

I’m doing two select, the category and subcategory. Both via Mysql. I would like to pass the category ID to the Subcategory when selecting the category.

$rsCategoria    = $conexao->query("SELECT * FROM categoria WHERE idioma = 'pt-br' ");
$rsSubcategoria = $conexao->query("SELECT * FROM categoriasub WHERE id_categoria = '{$id_categoria}' AND idioma = 'pt-br' ");

.

<select id="categoria" name="categoria" class="input form-control col-lg-10" required>
                        <option value="" selected="selected">Selecione</option>
                        <?php while ($rowCategoria = $rsCategoria->fetch_assoc()) {?>
                            <option value="<?php echo $rowCategoria['ID_Categoria'] ?>"><?php echo $rowCategoria['nome'] ?></option>
                        <?php } ?>
                    </select>

                    <select id="subcategoria" name="subcategoria" class="input form-control col-lg-10" style="margin-top: 20px;">
                        <option value="" selected="selected">Selecione</option>
                        <?php while ($rowSubcategoria = $rsSubcategoria->fetch_assoc()) {?>
                            <option value="<?php echo $rowSubcategoria['ID_Categoria'] ?>"><?php echo $rowSubcategoria['nome'] ?></option>
                        <?php } ?>
                    </select>

What would be the best way to search for the subcategory?

  • I usually use ajax request by placing an onchange event in the first <select> by sending the category code and the request response fills the <select> of the subcategory

  • @Wagnersoares How to mount while using AJAX?

  • You are trying to update the subcategory automatically when the user chooses the category that is?

  • @Exactly Isac. After selecting a category, load the subcategories.

  • The best option will be exactly what Wagnersoares indicated by Ajax not to refresh the entire page, and put the result directly on <select> if html or build <select> on the basis of a json reply.

  • @Isac How to mount while using AJAX or JSON?

Show 1 more comment

1 answer

1

I managed to, I’ll leave it on record here for anyone who has the same doubts as me.

SELECT

$rsCategoria    = $conexao->query("SELECT * FROM categoria");

HTML

<select id="categoria" name="categoria" required>
    <option value="" selected="selected">Selecione</option>
        <?php while ($rowCategoria = $rsCategoria->fetch_assoc()) {?>
            <option value="<?php echo $rowCategoria['ID_Categoria'] ?>"><?php echo $rowCategoria['nome'] ?></option>
        <?php } ?>
</select>

<select id="subcategoria" name="subcategoria" required>
    <option>Selecione acima primeiro</option>
</select>

JQUERY

$(document).ready(function () {
    $("select[name=categoria]").change(function(){
        $("select[name=subcategoria]").html('<option value="">Carregando...</option>');

        $.post("inc_subCategoria.php",
            {ID_Categoria:$(this).val()},
            function(valor){
                $("select[name=subcategoria]").html(valor);
           }
        )
    })
});

inc_subCategoria.php

<?php
include 'conexao.php';

$rs = $conexao->query("SELECT * FROM categoriasub WHERE id_categoria = '{$_POST['ID_Categoria']}' AND idioma = 'pt-br' ");

    echo '<option value="" selected="selected">Selecione</option>';
while($row = $rs->fetch_assoc()) {
    echo '<option value="'.$row['ID_Subcategoria'].'">'.$row['nome'].'</option>';
}

I hope this can help someone. If helped mark up.

Browser other questions tagged

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