Ajax not populating select on second request

0
Hello, I have some ajax requests on my page that populate a 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 ) );
?>
    
asked by anonymous 16.10.2018 / 18:01

1 answer

1

Why is your script tag within the Select tag?

When you do

$('#sub_categoria1').html('TEXTO'); or $('#sub_categoria1Modal').html('TEXTO');

You are deleting all your script, so the second time it does not work.

    
16.10.2018 / 20:38