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 ?
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);
}
I changed the code but it doesn’t work the same way
– user92870
@Victort. Oq does not work?
– Sam
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
– user92870
@Victort. Obg! Success!
– Sam