2
Hello guys, are you all right? My first question put here in the stack, I always consult the forum to remedy some doubts and always find what I’m looking for. I know there are some topics with the same title, but what I need I’m really not able to find.
I’m doing a little work in php, an electronic request for materials, the request works as follows the user type the product code and as soon as change field the product description is filled in, so far beauty I can do.
The problem is that I have a button to add more products and when clicking it clones the inputs and adds another line to the form and when entering another product code its description is not filled. My knowledge with php is basic to medium and with ajax and jquery I’m still pretty beginner.
I will post here the codes if you can help me I will be very grateful! Thank you
requisicoes.php (html)
<form id="clone-form" class="requisicao formrequisicao" method="post" action="">
<div class="col-7">
<label>Codigo
<input type="text" name="Cod" id="Cod" class="inputform" placeholder="Código do produto"/>
</label>
</div>
<div class="col-5">
<label>Descrição
<input type="text" name="cDesc" id="Desc" class="inputform" placeholder="Descrição do produto"/>
</label>
</div>
<div class="col-6">
<label>Necessidade Mensal
<input type="text" name="cDesc" id="cDesc" class="inputform" placeholder="Descrição do produto"/>
</label>
</div>
<div class="col-6">
<label>Saldo Atual
<input type="text" name="cDesc" id="cDesc" class="inputform" placeholder="Descrição do produto"/>
</label>
</div>
<div class="col-6">
<label>Solicitado
<input type="text" name="cDesc" id="cDesc" class="inputform" placeholder="Descrição do produto"/>
</label>
</div>
</div>
<div class="col-submit-2">
<input type="button" id="botao2" class="centro enviar clonador" value="Adicionar Produto">
</div>
<div class="col-submit-2">
<input type='submit' id='botao2' class='enviar' value='Enviar'/>
</div>
</form>
requisicao.php products
<?php
if(isset($_GET['Cod'])){
$cod = $_GET['Cod'];
}
$host = "localhost";
$db = "rem";
$user = "root";
$pass = "";
$conecta = new mysqli($host, $user, $pass, $db);
$conecta->query("
SET NAMES utf8;
");
if(!$conecta){
echo "Não foi possivel conectar no banco de dados";
}
date_default_timezone_set('America/Sao_Paulo');
$prod = $conecta->query("
SELECT * FROM tab_produto where codproduto = '".$cod."';
");
$linha = mysqli_fetch_array($prod);
$total = mysqli_num_rows($prod);
if($total > 0){
if($linha['tipo_id'] == 3){
$Desc = $linha['descricao'];
$produtos[$cod]['Desc'] = $Desc;
echo $produtos [$cod]['Desc'];
}
else if($linha['tipo_id'] == 4){
$produtos[$cod]['Desc'] = $linha['descricao'];
echo $produtos[$cod]['Desc'];
}
}else{
$produtos[$cod]['Desc'] = "Código não cadastrado";
echo $produtos[$cod]['Desc'];
}
Ajax and script to clone inputs
$(function () {
$("#Cod").blur(function () {
var Cod = $(this).val();
$.ajax({
type: "GET",
url: "../ajax/produtos-requisicao.php",
data: "Cod="+Cod,
success: function(produtos){
$("#Desc").val(produtos);
}
});
});
});
$(document).ready(function(){
var elm_html = $('#clone-form').html(); //faz uma cópia dos elementos a serem clonados.
$(document).on('click', '.clonador', function(e){
e.preventDefault();
var i = $('.requisicao').length; //pega a quantidade de clones;
var elementos = elm_html.replace(/\[[0\]]\]/g, '['+i+++']'); //substitui o valor dos index e incrementa++
$('#clone-form').append(elementos); //exibe o clone.
});
});
Thank you very much, it was exactly what I had talked yesterday with my teacher of the college, she said that when cloning the div the id of the inputs remained the same and that to solve I would have to find a way to increase the ids for them not to repeat themselves, Thanks guy you saved me, I was trying to find another solution, now I will continue here.
– danielcarlos