-2
I am developing a system where the user inserts lines as needed (this part is already ready). I’m having trouble generating PHP and the database to save the records, this is the first time I had to generate a input
dynamic. I attach the HTML code and the scripts used
<div class="box-content">
<script src="<?php echo INCLUDE_PATH_PAINEL ?>js/jquery.js"></script>
<!--script src="<?php echo INCLUDE_PATH_PAINEL ?>js/jquery.mask.js"></script>
<script src="<?php echo INCLUDE_PATH_PAINEL ?>js/jquery.mask.min.js"></script-->
<link href="<?php echo INCLUDE_PATH_PAINEL ?>css/css/select2.min.css" rel="stylesheet" />
<script src="<?php echo INCLUDE_PATH_PAINEL ?>js/js/select2.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/all.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/v4-shims.css">
<script>
jQuery(function($){
$('.CPF').mask('999.999.999-99');
$('.CNPJ').mask('99.999.999/9999-99');
$('#CPFconjuge').mask('999.999.999-99');
$('.RG').mask('AA-99.999.999');
//$('#ClienteDtNasc').mask('99/99/9999');
//$('#DataExp').mask('99/99/9999');
//$('#dt_exp_cart').mask('99/99/9999');
$('.Celular').mask('(99)99999-9999');
$('.Telefone').mask('(99)9999-9999');
$('#whatsapp').mask('(99)99999-9999');
$('#placa').mask('AAA-9999');
$('#CEP').mask('99999-999');
$('.moeda').mask('#.##0,00', {reverse: true});
});
</script>
<h2><i class="fas fa-bacon"></i> Cardápio <i class="fas fa-bacon"></i></h2>
<form method="post" enctype="multipart/form-data">
<fieldset>
<legend>Dados Produtos:</legend>
<label>Categoria:</label>
<select name="grupo" class="prod">
<option disabled="" selected="">Selecione</option>
<option value="Alimento">Alimento</option>
<option value="Bebida">Bebida</option>
</select>
<label>Item:</label>
<input type="text" name="nome_item">
<label>Preço Unitário:</label>
<input type="text" name="preco_compra" class="moeda">
<label>Descrição:</label>
<textarea name="desc"> </textarea>
</fieldset>
<fieldset>
<legend>Ingredientes:</legend>
<table id="products-table">
<tr>
<th>Produto</th>
<th>Quantidade Utilizada</th>
<th>Remover</th>
<tr>
<tr class="row">
</tr>
</table>
<div class="botoes" style="text-align: center">
<button onclick="AddTableRow()" type="button"><i class="fas fa-utensil-spoon"></i> Adicionar ingrediente</button>
<!--button onclick="RemoveTableRow()" type="button"><i class="fas fa-trash-alt"></i> Remover</button-->
</div>
</fieldset>
<div class="botoes">
<div class="clear"></div>
<div><button type="submit" name="acao">Enviar</button></div>
<div><button type="reset" style="margin:15px;" class="bt">Limpar</button></div>
</div>
<div class="clear"></div>
</form>
<script>
$('.prod').select2({
});
</script>
<script>
RemoveTableRow = function(handler) {
var tr = $(handler).closest('tr');
tr.fadeOut(400, function() {
tr.remove();
});
return false;
};
let num = 1; //num criado aqui
AddTableRow = function() {
var newRow = $("<tr>");
var cols = "";
cols += '<td class="col-md-3"><select class="form-control prod produto" name="produto'+ num + '"> <option selected="" disabled="">Selecione </option> <?php
$result = MySql::conectar()->prepare("SELECT * FROM `produto`");
$result->execute();
foreach($result as $valor){
?> <option value="<?php echo $valor['ID']?>"><?php echo $valor['nome_produto'] ?></option><?php
}
?></select></td>';
cols += '<td class="col-md-3"><input type="text" class="form-control qtd_usada" name="qtd_usada' + num + '"></td>';
cols += '<td class="col-md-3">'+'<a class="btn delete" onclick="RemoveTableRow(this)" type="button"><i class="fas fa-trash-alt"></i></a>'+ '</td>';
num++; //numero aumenta aqui
newRow.append(cols);
$("#products-table").append(newRow);
return false;
};
</script>
<script>
/*function ajustarNomes(){
$(".table tr").each(function(indice){
$(this).find('.produto').attr("name", "produto" + indice);
$(this).find('.qtd_usada').attr("name", "qtd_usada" + indice);
});
}
$(".amount, .price").unbind('blur keyup');
$(".amount, .price").on("blur keyup",function(){
const tr = $(this).parent().parent();
const quant = parseInt(tr.find('.amount').val());
const valor = parseInt(tr.find('.price').val());
var total = quant * valor;
if (!isNaN(quant) && !isNaN(valor)){
tr.find('.total').html('<input readonly type="text" class="form-control" name="total" value=" '+total+' ">');
}
}
*/
</script>
Monique is not clear his difficulty, is having trouble picking up the form values after saving?
– Glenys Mitchell