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);
}