0
I am set up a request system that takes information from the database and fills the fields with it.
Follow the request code that fills the fields with the information:
$(document).ready(function(){
$(".nome_prod").on("focusout", function(){
var $cod_prod = $(".cod_prod");
var $quantidade = $(".quantidade");
var $valor_uni = $(".valor_uni");
$.getJSON('function2.php',{
nome_prod: $( this ).val()
},function( json ){
$cod_prod.val( json.cod_prod );
$quantidade.val( json.quantidade );
$valor_uni.val( json.valor_uni );
});
});
});
The code that clones the fields:
function duplicarCampos(){
var clone = document.getElementById('origem').cloneNode(true);
var destino = document.getElementById('destino');
destino.appendChild (clone);
var camposClonados = clone.getElementsByTagName('input');
for(i=0; i<camposClonados.length;i++){
camposClonados[i].value = '';
}
}
function removerCampos(id){
var node1 = document.getElementById('destino');
node1.removeChild(node1.childNodes[0]);
}
and the function2.php
include_once("connections/conn2.php");
function retorna($nome_prod, $conn1){
$result_prod = "SELECT * FROM cad_produtos WHERE nome_prod = '$nome_prod' LIMIT 1";
$resultado_prod = mysqli_query($conn1, $result_prod);
if($resultado_prod->num_rows){
$row_prod = mysqli_fetch_assoc($resultado_prod);
$valores['nome_prod'] = $row_prod['nome_prod'];
$valores['quantidade'] = $row_prod['uni_med'];
$valores['valor_uni'] = $row_prod['valor_uni'];
$valores['cod_prod'] = $row_prod['codigo_prod'];
}
else{
}
return json_encode($valores);
}
if(isset($_GET['nome_prod'])){
echo retorna($_GET['nome_prod'], $conn1);
}
When I duplicate the field it does not work, but when I click on the first field it fills all others with the same information contained in the first field.
The fields that are cloned are in the original div and are cloned to target div but they only copy the information from the first field
<div id="origem">
<div class="row">
<div class="col-md-2 col-xs-2">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fa fa-barcode"></i>
</div>
</div>
<input id="cod_prod" name="cod_prod[]" placeholder="Codigo do produto" type="text" class="form-control cod_prod">
</div>
</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="form-group ">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fa fa-align-justify"></i>
</div>
</div>
<input id="descricao" name="nome_prod[]" placeholder="Descrição" type="text" class="form-control nome_prod">
</div>
</div>
</div>
<div class="col-md-2 col-xs-2">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fa fa-money"></i>
</div>
</div>
<input id="valor" name="valor_uni[]" placeholder="Valor" type="text" class="form-control valor_uni">
</div>
</div>
</div>
<div class="col-md-2 col-xs-2">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fa fa-money"></i>
</div>
</div>
<input id="valor" name="quantidade[]" placeholder="Quantidade" type="text" class="form-control quantidade">
</div>
</div>
</div>
</div>
</div>
<div id="destino">
</div>
@Leonardobuta this question I mentioned to you
– Daniel Ricardo
I can’t analyze it now to give an answer. But before anyone can do it, improve the question, it is confused, it is difficult to understand both the goal and the error.
– tvdias
Let me get this straight. You have a page that returns data from your database in a form. Now you want to create a way to duplicate that data to create a new entry in the database, that’s it?
– tvdias
I updated the question of a look there please
– Daniel Ricardo
i have a code that completes the fields with information coming from the bank, I just type the name and the information completes the other input fields but it is not completing there when I click on the first field it completes but copying the other information
– Daniel Ricardo
You’ve made it easy enough to understand.
– tvdias
Not related to the question, but already a framework to make your work easier? Something like Laravel or Cakephp, for example?
– tvdias
so I’ve never used any framework, I know it makes it easier but as a beginner I have a lot to learn yet
– Daniel Ricardo
I already see something wrong: when you clone the div
#origem
, you’re duplicating both the id#origem
as the id’s of the elements within that div. If you’re still learning HTML, know that an id should be unique on the page, meaning there can’t be two elements with the same id#origem
(or other id’s). Useclass
instead.– Sam
I know it was just inattention
– Daniel Ricardo
the problem is that this not giving id conflict as usual
– Daniel Ricardo