Function filling only one line

Asked

Viewed 32 times

2

I created a function so that as the user type a client number, is filled the fancy name of it, but as this data comes within a table and I use array to capture (fazer insert e update) in this data, all lines are filled instead of the only line I typed, someone knows how to fix it ?

inserir a descrição da imagem aqui

table structure

<?php for($i = 0; $i <= 5; $i++){ //coloquei este valor para testar ?>        
   <tr>                                                   
        <input type="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);
}

1 answer

0


First thing, would have has to change the id="cadcli_codigo" for class="cadcli_codigo", for in the loop for is generating several elements with the same id, and this, in addition to being outside the standards (one id should be unique on a page1) is generating conflict in the capture of onchange.

Another thing is to change the selector line below:

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

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

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

With these changes, your code should work perfectly.

1 There are several "Josés" in the world, but each José has one fingerprint different from the other. It’s like in HTML: there can be several divs, inputs etc., but none of them should have the same id on the same page. In the case of class is different: there may be several "Josés" bald in the world. As in HTML, there can be several elements with the same class.

  • I changed the code but it doesn’t work the same way

  • @Victort. Oq does not work?

  • I vacillated, I will edit the code above, realize that you are with # instead of . to reference cadcli_codigo, sorry, thanks for the help I will put as the best response guy and thanks for sharing your wisdom also rs

  • 1

    @Victort. Obg! Success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.