I am a layman in the world of Node.JS and I am developing a form in which the number of fields is dynamic, ie there is a button that calls a function in JavaScript that is adding fields according to the user's need.
My problem is to capture this " array " field and pass it correctly to my save function so that all content is inserted into the database.
HTML form code:
<html>
<body>
<form action="/boletim/salvar" method="post">
<div class="formulario">
<div class="input-group control-group">
<input type="text" name="data_evento" class="form-control" id="datetime" placeholder="Data do evento">
</div>
<div class="input-group control-group col-md-4">
<input type="text" name="titulo_evento" class="form-control" placeholder="Título do evento">
</div>
</div>
<div class="copy-fields hide">
<div class="control-group">
<div class="input-group" style="margin-top:10px">
<input type="text" name="data_evento" class="form-control calendar" placeholder="Data do evento" id="datetime" onmouseover="calendario()">
</div>
<div class=" input-group col-md-4">
<input type="text" name="titulo_evento" class="form-control" placeholder="Título do evento">
</div>
<div class="input-group-btn after-add-more">
<button class="btn btn-danger remove" type="button">
<i class="glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
<button type="submit" class="btn btn-success">Salvar</button>
</form>
</body>
</html>
JavaScript function ( front-end ):
//Adiciono a div que contem os campos
$(".add-more").click(function () {
var html = $(".copy-fields").html();
$(".copy-fields").before(html);
//$(".formulario").after(html);
});
//Removo a div que contem os campos que foram adicionados
$("body").on("click", ".remove", function () {
$(this).parents(".control-group").remove();
});
$("#datetime").datetimepicker({
format: "yyyy-mm-dd hh:ii",
autoclose: true
});
back-end module:
module.exports.salvar = function(application,req,res){
var connection = application.config.dbConnection();
var boletimModel= new application.app.models.BoletimDAO(connection);
var info_boletim = req.body;
boletimModel.salvarEventos(info_boletim,function(error, result){
res.redirect('/');
});
}
Content that the " save " module is receiving:
{ data_evento: [ '2018-03-01 23:50', '2018-03-02 07:35', '' ],
titulo_evento: [ 'Conteudo do primeiro campo', 'conteudo do segundo campo', '' ] }
Any suggestions how to best solve this problem?