1
Searching, I was able to find a script to create and remove fields.
Link Add and Remove Fields with Javascript
My question now, is how to send this data to the database, for a future query and display?
My version:
$(function () {
var divContent = $('#formulario');
var botaoAdicionar = $('a[data-id="1"]');
var i = 1;
//Ao clicar em adicionar ele cria uma linha com novos campos
$(botaoAdicionar).click(function () {
$('<div class="conteudoIndividual"><div class="row"><div class="col-xs-5"><div class="form-group" style="margin:0 0 0 1px;"><label class="control-label">Veículo</label><select name="veiculo[]" id="" class="form-control"><option value="S">Sim</option><option value="N">Não</option></select></div></div><div class="col-xs-2"><div class="form-group" style="margin:0 0 0 1px;"><label class="control-label">Valor</label><input name="veiculoValor[]" type="text" class="form-control" id="" value="" required></div></div></div><a href="#" class="linkRemover">- Remover Campos</a></div>').appendTo(divContent);
$('#removehidden').remove();
i++;
$('<input type="hidden" name="quantidadeCampos" value="' + i + '" id="removehidden">').appendTo(divContent);
});
//Cliquando em remover a linha é eliminada
$('#formulario').on('click', '.linkRemover', function () {
$(this).parents('.conteudoIndividual').remove();
i--;
});
});
<a href="#" data-id="1" id="adicionarCampo">+ adicionar campo</a>
<form action="teste.php" method="post">
<div id="formulario">
<div class="row">
<div class="col-xs-5">
<div class="form-group" style="margin:0 0 0 1px;">
<label class="control-label">Veículo</label>
<select name="veiculo[]" id="" class="form-control">
<option value="S">Sim</option>
<option value="N">Não</option>
</select>
</div>
</div>
<div class="col-xs-2">
<div class="form-group" style="margin:0 0 0 1px;">
<label class="control-label">Valor</label>
<input name="veiculoValor[]" type="text" class="form-control" id="" value="" required>
</div>
</div>
</div>
</div>
<input type="hidden" name="MM_insert" value="form1">
<input type="submit" value="Gerar Código" />
</form>
I edited your question in order to bring up the code of jsfiddle here. I don’t know why
adicionar campo
works there and in the snippet nay.– ramaral
Mr @ramaral knows how to send to BD?
– Tiago
What’s in the 'test.php' script'?
– gabrieloliveira
nothing, is the html file itself
– Tiago
This is the script to save to the database. You will use Mysql?
– gabrieloliveira
Yes Mr bd mysql.
– Tiago
You already have tables created in Mysql?
– gabrieloliveira
Let’s go continue this discussion in chat.
– Tiago
Not yet because I don’t know what would be the best way. I was thinking of sending as an array, this would be the right one?
– Tiago
I created it by hand, I don’t know if it’s right. In theory this is the best way? Send everything to just one field in the table http://i.imgur.com/X9eidpn.png
– Tiago
Next: You need to first create the table that will receive these values. With the table, you will receive on the page an array with these form values. You will go through each one. Ex: foreach ($_POST['vehicle'] as $key=>$value){mysql_query("INSERT INTO table (vehicle, vehicleValue) VALUES ('$value', '{$_POST['vehicleValue'][$key]}');}
– gabrieloliveira
But that will create a lot of fields, right? I was thinking of creating a single field to receive data via array, like this image I created at hand http://i.imgur.com/X9eidpn.png
– Tiago
@gabrieloliveira saw what I wrote?
– Tiago
I’m in the chat room, open up.
– gabrieloliveira
Como faço o foreach desses dados? $veiculo = a:2:{i:0;a:2:{i:0;s:1:"S";i:1;s:1:"S";}i:1;a:2:{i:0;s:6:"350.00";i:1;s:6:"400.00";}}
– Tiago
Does not work foreach($vehicle as $key => $val){ echo "$val"; }
– Tiago