2
Friends, I’m having trouble recording cloned Mysql fields in the form. I’ll try to explain in detail to see if my friends can help me.
<div class="mb-md">
<a href="#" id="but_add">
<i class="fa fa-plus text-primary"></i>
<span class="text-semibold text-primary"> Adicionar Categoria</span>
</a>
</div>
<div class="categoria row" id="categoria">
<section class="panel panel-border">
<header class="panel-heading">
<div class="pull-right">
<a href="#" class="text-primary text-semibold remove-categoria">Excluir</a>
</div>
<input type="text" name="ds_categoriacomp[]" class="form-control-small-height" placeholder="Nome" data-plugin-maxlength maxlength="100" required>
<div class="row col-padding-top-x15">
<div class="col-md-6">
<div class="form-group">
<label class="col-md-1 control-label">Qtd.</label>
<div class="col-md-8">
<div class="input-group">
<span class="input-group-addon">min</span>
<input type="number" id="qtd_min" name="qtd_min[]" value="0" min="0" max="99" maxlength="2" step="1" required="required" class="form-control-small-height qtd_min"/>
<span class="input-group-addon">máx</span>
<input type="number" id="qtd_max" name="qtd_max[]" min="1" max="99" maxlength="2" step="1" required="required" class="form-control-small-height"/>
</div>
</div>
</div>
</div>
<div class="col-md-4 pull-right custom-padding-top-x7">
<div class="checkbox-custom checkbox-default">
<input type="checkbox" name="cd_obrigatorio[]"/>
<label>Obrigatório</label>
</div>
</div>
</div>
</header>
<div class="panel-body">
<div class="row" style="margin-top:-15px;">
<table class="table mb-none tb-complementos" id="tb-complementos">
<tr>
<td class="actions col-sm-2">
<input id="ds_complemento" name="ds_complemento[]" placeholder="Nome" class="form-control-small-height" data-plugin-maxlength maxlength="100" required>
</td>
<td class="actions col-lg-4">
<input id="ds_descComp" name="ds_desccomp[]" placeholder="Descrição" class="form-control-small-height" data-plugin-maxlength maxlength="2000" required>
</td>
<td class="actions text-center col-lg-1">
<input id="vl_valorcomp" name="vl_valorcomp[]" class="form-control-small-height" placeholder="Preço" onkeyup="formatarMoeda(this);" onChange="carregaPrValor(this.value);" required>
</td>
<td class="actions text-center col-lg-1">
<a href="#" class="remove-complemento">
<span class="text-semibold">Excluir</span>
</a>
</td>
</tr>
</table>
<a href="#" id="but_add_c" class="but_add_c" style="padding-left:10px;">
<i class="fa fa-plus text-dark"></i>
<span class="text-semibold text-dark"> Adicionar complemento</span>
</a>
</div>
</section>
</div>
The HTML above generates form in the image below:
By clicking on Add complement, js function makes a clone of the complement fields as shown below:
By clicking on Add category js function generates a clone of the full form, where within this "clone" form I can add more complements and so on. Figure below:
This is where the problem occurs. How can I save in the database since there are two tables, Tabela1 saves the category name and table 2 saves the add-ons.
Below the way I am trying to do in PHP, but without success:
for($i = 0; $i < count($nomecategoria); $i++){
mysql_query("insert into tabela1 (nomecategoria, valor1, valor2...)values('".$nomecategoria[$i]."', '".$valor1[$i]."', '".$valor2[$i]."')");
//busca id da categoria gerado no insert
$idcategoria = mysql_insert_id();
}
for($c = 0; $c < count($nomecomplemento); $c++){
mysql_query("insert into tabela2 (idcategoria, nomecomplemento, descricao)values('".$idcategoria."', '".$nomecomplemento[$c]."', '".$descricao[$c]."')");
}
The above code saves the categories in the Tabela1 correctly, but when saving the complements in the table 2 the idcategoria is always the same.
Javascript that clones objects:
//add categoria complemento/////////////////////////////////
$(document).ready(function(){
$('#but_addcategoria').click(function(){
var newel = $('.categoria:last')
.clone(true)
.find("input").val("").end()
$(newel).insertAfter(".categoria:last");
});
});
//add item complemento
$(document).ready(function(){
$('#but_addcomplemento').click(function(){
var newel = $('.tb-complementos:last')
.clone(true)
.find("input").val("").end();
$(newel).insertBefore(this);
});
});
There is an ideal way to make these Inserts so that each add-on gets its own idcategoria correct?
Already thank the friends who try to help.
Thanks for the answer Sam. My js cloning the object is quite simple, I added the code at the end of the question. Would you have any tips on how to create indexes and subindexes in add-ons arrays? !
– Maicon
I’ll try to do it here and warn you.
– Sam
I updated the answer.
– Sam
Thank you very much Sam, I will test and then inform here the result.
– Maicon
Sam, now it was right the idcategory of each complement, however the values ds_complement, ds_desccomp and vl_valorcomp, save in the base a string Array. No for do PHP did as you said. Strange pq the other fields save correctly.
– Maicon
Well, that’s something. Let’s go after the problem with the array now.
– Sam
Yes, it’s enough. The way I was doing was very wrong.
– Maicon
I think the
$nomecomplemento[$c]
and the$descricao[$c]
should have the sub-index:$nomecomplemento[$i][$c]
and$descricao[$i][$c]
– Sam
Okay, it worked perfectly, that’s exactly it, the sub-index was missing in the variables. Thank you very much Sam, solved my problem I’ve been hitting for days.
– Maicon