3
By clicking the button next to UF the user opens a table with the states of Brazil, and from the choice of the state the other fields are completed, until then all well I was able to do, but I have to click inside the input and then elsewhere or give TAB, To complete the fields, is there any way that I can adapt my function to complete the field immediately, without having to give TAB or anything like ? To make the user’s life easier
index php.
<script type="text/javascript">
$(document).ready(function(){
$("input[name='estado']").on("change", function(){
var $nome_estado = $("input[name='nome_estado']");
var $aliq_base_icms = $("input[name='aliq_base_icms']");
var $aliq_icms = $("input[name='aliq_icms']");
var $icms_subst_sn = $("input[name='icms_subst_sn']");
var $insc_est_subst = $("input[name='insc_est_subst']");
var $aliq_icms_subst = $("input[name='aliq_icms_subst']");
$.getJSON('function_est.php',{
estado: $ ( this ).val()
},function( json ){
$nome_estado.val ( json.nome_estado );
$aliq_base_icms.val ( json.aliq_base_icms );
$aliq_icms.val ( json.aliq_icms );
$icms_subst_sn.val ( json.icms_subst_sn );
$insc_est_subst.val ( json.insc_est_subst );
$aliq_icms_subst.val ( json.aliq_icms_subst );
});
});
});
</script>
<div class="col-lg-12">
<div class="col-lg-3 input-group" style="padding-left:16px;"><!-- Inicio Input ID -->
<label for="ex1">UF: </label>
<input type="text" class="form-control estado" name="estado">
<span class="input-group-btn">
<button class="btn btn-secondary" style="margin-top:25px;" type="button" data-toggle="modal" data-target="#Modal">...</button>
</span><br>
</div><br>
</div>
<div class="col-lg-12">
<div class="col-lg-5"><!-- Inicio Input ID -->
<label for="ex1">Estado: </label>
<input type="text" class="form-control" name="nome_estado"><br>
</div>
</div>
<div class="col-lg-12">
<div class="col-lg-3"><!-- Inicio Input ID -->
<label for="ex1">Alíquota Base do ICMS: </label>
<input type="text" class="form-control" name="aliq_base_icms"><br>
</div>
</div>
<div class="col-lg-12">
<div class="col-lg-3"><!-- Inicio Input ID -->
<label for="ex1">Alíquota do ICMS: </label>
<input type="text" class="form-control" name="aliq_icms"><br>
</div>
</div>
<script>
$(document).on('click', '.get-estado', function() {
var value = $(this).text();
$('.close').trigger('click');
$('.estado').val(value);
});
$(document).on('click', '.get-nome_estado', function() {
var value = $(this).siblings('.get-estado').text();
$('.close').trigger('click');
$('.estado').val(value);
});
</script>
function_est.php
<?php
include_once("conn.php");
function retorna($estado, $conn){
$result = "SELECT * FROM estados WHERE estado = '$estado' ";
$resultado = mysqli_query($conn, $result);
// DECLARA A VARIAVEL
$valores = array();
if($resultado){
$row = mysqli_fetch_assoc($resultado);
$valores['nome_estado'] = $row['nome_estado'];
$valores['aliq_base_icms'] = $row['aliq_base_icms'];
$valores['aliq_icms'] = $row['aliq_icms'];
$valores['icms_subst_sn'] = $row['icms_subst_sn'];
$valores['insc_est_subst'] = $row['insc_est_subst'];
$valores['aliq_icms_subst'] = $row['aliq_icms_subst'];
} else {
return json_encode(array( 'error' => mysqli_error($conn) ));
}
return json_encode($valores);
}
if(isset($_GET['estado'])){
echo retorna($_GET['estado'], $conn);
}
?>
What code do you use for the modal to enter the selected value in the input?
– Gabriel Heming
I will put below the html code, but it captures the data coming from the modal normally
– user92870