0
Hello, I have some ajax requests on my page that populate a select
correctly, however, I need you to do it again in an update modal, but the same ajax request returns nothing.
This is the code of 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>
<script type="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 doesn’t. This is the second time I’ve requested.
<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>
<script type="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 the 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 ) );
?>
I did a test with your code, ignoring only SQL and returning a static array. It worked perfectly. I advise you to use a Try catch to catch any kind of error and/or look at apache/Nginx logs.
– Patrick Maciel
Hello Patrick, did all ajax requests work? here does not work at all
– Caio Brandão
I don’t understand much about what you said, could give a strength?
– Caio Brandão