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
– Wagner Soares
@Wagnersoares How to mount while using AJAX?
– Tiago
You are trying to update the subcategory automatically when the user chooses the category that is?
– Isac
@Exactly Isac. After selecting a category, load the subcategories.
– Tiago
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
@Isac How to mount while using AJAX or JSON?
– Tiago