Functions Conflicting When Returning Array

0

I have two functions that are triggered from the moment the user types a product code, however they are conflicting, could anyone tell me what to do? For, within the JSON, in Inspect = > Aba Network , I have these results that would be the ones that should fill the tables. I'm suspecting this line $("table tbody") ...

This should return within the 1st table

[{"recnum":"66","un_medida":"BD","operacao":"11.00","fator":"12.0000","default_venda":"13","fator_carga":"14.0000"}]

This should return within the 2nd table

[{"id":"516","codigo_tipo":"4","descricao":"5"},{"id":"517","codigo_tipo":"7","descricao":"7"},{"id":"518","codigo_tipo":"9","descricao":"9"}]

But I'm having this as a return

Index.php

$(document).ready(function(){$("input[name='codigo_produto']").on("change", function(){

                    $.getJSON('function_pro-1.php',{ 
                            codigo_produto: $( this ).val(),
                            executar : 2

                            },function( json ){
                                var x = "";
                                    for (var i = 0; i < json.length; i++) {

                                        x += '<tr>';
                                            x += '<input type="hidden" name="recnum[]" value="'+json[i].recnum+'" style="border:none; width:100%; background-color: transparent;">';
                                            x += '<td><input type="text" name="un_medida[]" value="'+json[i].un_medida+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="operacao[]" value="'+json[i].operacao+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="fator[]" value="'+json[i].fator+'" style="border:none; width:100%; background-color: transparent;"/></td>';     
                                            x += '<td><input type="text" name="default_venda[]" value="'+json[i].default_venda+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="fator_carga[]" value="'+json[i].fator_carga+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                        x += '</tr>';
                                    }
//
                                $("table tbody").html(x);
                        });
                });
        });

    $(document).ready(function(){
                $("input[name='codigo_produto']").on("change", function(){

                        $.getJSON('function_pro-1.php',{ 
                                codigo_produto: $( this ).val(),
                                executar : 3
                        },function( json ){
                                var html = "";
                                    for (var i = 0; i < json.length; i++) {

                                        html += '<tr>';
                                        html += '<input type="hidden" name="id[]" value="'+json[i].id+'" style="border:none; width:100%; background-color: transparent;">';
                                        html += '<td><input type="text" name="codigo_tipo[]" value="'+json[i].codigo_tipo+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                        html += '<td><input type="text" name="descricao[]" value="'+json[i].descricao+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                        html += '</tr>';
                                    }

                                $("table tbody").html(html);
                        });
                });
        });

function_pro-1.php

function convunid($codigo_produto, $conn){

           $result = "SELECT * FROM CONVUNID WHERE codigo_produto = '$codigo_produto' ";

           $resultado = $conn->query($result);

           // DECLARA A VARIAVEL
           $valores = array();

        if($resultado){
            $row = mysqli_fetch_assoc($resultado);
            $novo['recnum'] = $row['recnum'];
            $novo['un_medida'] = $row['un_medida'];
            $novo['operacao'] = $row['operacao'];    
            $novo['fator'] = $row['fator'];
            $novo['default_venda'] = $row['default_venda'];
            $novo['fator_carga'] = $row['fator_carga'];

            array_push($valores, $novo);

        } else {
         return json_encode(array( 'error' => mysqli_error($conn) ));        
        }

         return json_encode($valores);
    }

    $opc = isset($_GET['executar']) ? (int) $_GET['executar'] : 0;

     if($opc === 2){
        echo convunid($_GET['codigo_produto'], $conn);
    }

 /* ---------------------------------------TIPOPROD---------------------------------------------------------------------- */    
     function tipoprod($codigo_produto, $conn){

        $result = "SELECT * FROM tipoprod WHERE codigo_produto = '$codigo_produto' ";

        $resultado = mysqli_query($conn, $result);

        // DECLARA A VARIAVEL
        $valores = array();

        if($resultado){
            while( $row = mysqli_fetch_assoc($resultado)){

                $novo['id'] = $row['id'];
                $novo['codigo_tipo'] = $row['codigo_tipo'];
                $novo['descricao'] = $row['descricao'];

                array_push($valores, $novo);
            }

        } else {
            return json_encode(array( 'error' => mysqli_error($conn) ));        
        }

        return json_encode($valores);                
    }

    $opc = isset($_GET['executar']) ? (int) $_GET['executar'] : 0;

    if($opc === 3){
        echo tipoprod($_GET['codigo_produto'], $conn);
    }
    
asked by anonymous 30.10.2017 / 12:31

1 answer

0

In order not to conflict, you should do what I already imagined as I mentioned in the question, to make the table bodies do not conflict. So I put an ID in the body of my tables.

$(document).ready(function(){
            $("input[name='codigo_produto']").on("change", function(){
             
                    $.getJSON('function_pro-2.php',{ 
                            codigo_produto: $( this ).val(),
                            executar : 2

                            },function( json ){
                                var x = "";
                                    for (var i = 0; i < json.length; i++) {

                                        x += '<tr>';
                                            x += '<input type="hidden" name="recnum[]" value="'+json[i].recnum+'" style="border:none; width:100%; background-color: transparent;">';
                                            x += '<td><input type="text" name="un_medida[]" value="'+json[i].un_medida+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="operacao[]" value="'+json[i].operacao+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="fator[]" value="'+json[i].fator+'" style="border:none; width:100%; background-color: transparent;"/></td>';     
                                            x += '<td><input type="text" name="default_venda[]" value="'+json[i].default_venda+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                            x += '<td><input type="text" name="fator_carga[]" value="'+json[i].fator_carga+'" style="border:none; width:100%; background-color: transparent;"/></td>';
                                        x += '</tr>';
                                    }
//
                                $("#conv").html(x);
                        });
                });
        });
        

<tbody id='conv'>
  <?php for($i = 1; $i <= 5; $i++){ //coloquei este valor para testar ?>        
      <tr>                                                   
         <td><input type="text" maxlength=""  name="un_medida[]" style="border:none; width:100%; background-color: transparent;"></td></td>                                                    
         <td><input type="text" onkeyup="limitarInput(this)" name="operacao[]" style="border:none; width:100%; background-color: transparent;"></td>
         <td><input type="text" maxlength=""  name="fator[]" style="border:none; width:100%; background-color: transparent;"></td>
         <td><input type="text" onkeyup="limitarInput(this)" name="default_venda[]" style="border:none; width:100%; background-color: transparent;"></td>
         <td><input type="text" maxlength=""  name="fator_carga[]" style="border:none; width:100%; background-color: transparent;"></td>
      </tr>
  <?php } ?>
</tbody>
    
30.10.2017 / 14:21