select
correctly, however, I need to do it again in a modal update, but the same ajax request returns nothing.
This is the code for the first request:
<div class="form-group col-md-6">
<label class="col-md-3 control-label">Categoria</label>
<div class="col-md-9">
<select class="form-control" name="categoria" id="categoria" required>
<option>--- Selecione ---</option>
<?php
$nivel = 0;
$select = "SELECT * FROM categoria_recursiva WHERE nivel = '$nivel'";
$result_select = $conn->query($select);
while ($col = mysqli_fetch_assoc($result_select)):
echo '<option value="' . $col['id'] . '">' . $col['nome'] . '</option>';
endwhile;
?>
</select>
</div>
<div class="form-group col-md-6">
<label class="col-md-3 control-label">Sub Categoria1</label>
<div class="col-md-9">
<select class="form-control" id="sub_categoria1" name="sub_categoria">
<option>--- Selecione ---</option>
<script src="http://www.google.com/jsapi"></script><scripttype="text/javascript">
google.load('jquery', '1.3');
</script>
<script type="text/javascript">
$(function () {
$('#categoria').change(function () {
if ($(this).val()) {
$.getJSON('functions/pega_categoria.php?search=', {
id: $(this).val(),
ajax: 'true'
}, function (j) {
var options = '<option value=""></option>';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].id + '">' + j[i].nome + '</option>';
}
$('#sub_categoria1').html(options);
});
} else {
$('#sub_categoria1').html('<option value="">--- Selecione ---</option>');
}
});
});
</script>
</select>
</div>
This first works correctly, but the second does not. This is the second time I require it.
<div class="form-group col-md-6">
<label class="col-md-3 control-label">Categoria</label>
<div class="col-md-9">
<select class="form-control" name="categoria" id="categoriaModal" required>
<option>--- Selecione ---</option>
<?php
$nivel1 = 0;
$select1 = "SELECT * FROM categoria_recursiva WHERE nivel = '$nivel1'";
$result_select1 = $conn->query($select1);
while ($col1 = mysqli_fetch_assoc($result_select1)):
echo '<option value="' . $col1['id'] . '">' . $col1['nome'] . '</option>';
endwhile;
?>
</select>
</div>
</div>
<div class="form-group col-md-6">
<label class="col-md-3 control-label">Sub Categoria1</label>
<div class="col-md-9">
<select class="form-control" id="sub_categoria1Modal" name="sub_categoria">
<option>--- Selecione ---</option>
<script src="http://www.google.com/jsapi"></script><scripttype="text/javascript">
google.load('jquery', '1.3');
</script>
<script type="text/javascript">
$(function () {
$('#categoriaModal').change(function () {
if ($(this).val()) {
$.getJSON('functions/pega_categoria.php?search=', {
id: $(this).val(),
ajax: 'true'
}, function (j) {
var options = '<option value=""></option>';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].id + '">' + j[i].nome + '</option>';
}
$('#sub_categoria1Modal').html(options);
});
} else {
$('#sub_categoria1Modal').html('<option value="">--- Selecione ---</option>');
}
});
});
</script>
</select>
</div>
</div>
This is php:
<?php include("conexao.php");
$id_cat_selecionada = $_REQUEST['id'];
$sub_categoria = array();
$sql = "SELECT id,nome FROM categoria_recursiva WHERE id_pai=$id_cat_selecionada";
$res = $conn->query($sql);
while ( $col_cat = mysqli_fetch_assoc( $res ) ) {
$sub[] = array(
'id' => $col_cat['id'],
'nome' => $col_cat['nome'],
);
}
echo( json_encode( $sub ) );
?>