Function filling only one line

2

I created a function so that as the user enters a client number, the fancy name of the client is filled in, but since this data comes inside a table and I use array to capture ( fazer insert e update ) in that data, all lines fill in instead of the single line I typed, does anyone know how to fix this?

tablestructure

<?phpfor($i=0;$i<=5;$i++){//coloqueiestevalorparatestar?><tr><inputtype="hidden" maxlength="6" name="recnum[]" value="<?php $row['codigo_produto'] ?>">
        <td><input type="text" maxlength="" name="produto_cliente[]" style="border:none; width:100%; background-color: transparent;"></td>
        <td><input type="text" maxlength="" class="cadcli_codigo" name="cadcli_codigo[]" style="border:none; width:100%; background-color: transparent;"></td>
        <td><input type="text" maxlength="" name="nome_fantasia[]" style="border:none; width:100%; background-color: transparent;"></td>
        <td><input type="text" maxlength="" name="emitente_nfe[]" class="emitente_nfe"  style="border:none; width:100%; background-color: transparent;"></td>       
        <td><input type="text" maxlength="" name="peso_bandeija[]" placeholder="0,0000" style="border:none; width:100%; background-color: transparent;"></td>   
   </tr>
<?php } ?>

index.php

$(document).ready(function(){
        $(".cadcli_codigo").on("change", function(){

                    var $nome_fantasia = $(this).closest("tr").find("input[name='nome_fantasia[]']");

                    $.getJSON('function_pro-1.php',{ 
                            cadcli_codigo: $( this ).val()
                    },function( json ){                          
                            $nome_fantasia.val ( json.nome_fantasia );
                    });
            });
    });

function_pro-1.php

function nome($cadcli_codigo, $conn){

    $result = "SELECT * FROM cadcli WHERE codigo = '$cadcli_codigo' ";

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

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

    if($resultado){

        $row = mysqli_fetch_assoc($resultado);
        $valores['nome_fantasia'] = $row['nome_fantasia'];

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

    return json_encode($valores);                
}

if(isset($_GET['cadcli_codigo'])){
    echo nome($_GET['cadcli_codigo'], $conn);
}
    
asked by anonymous 01.11.2017 / 18:17

1 answer

0
First thing, would have to change the id="cadcli_codigo" to class="cadcli_codigo" , because in loop for is generating multiple elements with the same id and this, in addition to being out of standards ( a id must be unique on a page 1 ) is generating conflict in%

Another thing is to change the selector of the line below:

var $nome_fantasia = $("input[name='nome_fantasia[]']");

for the line below, so that the onchange to receive the value is the same as the line of input that the information was entered (in this case, the client code):

var $nome_fantasia = $(this).closest("tr").find("input[name='nome_fantasia[]']");

With these changes, your code should work perfectly.

  

1 There are several "Joses" in the world, but each Joseph has a    fingerprint different from the other. It's like in HTML: there may be several input , divs etc., but none of them should have the same inputs on a same page. In the case of id it is different: there may be several bald in the world. Like HTML, there can be multiple elements with the same class .

    
01.11.2017 / 19:15