Save form with Nodejs and Mysql

Asked

Viewed 839 times

0

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 adds fields according to the user’s need.

My problem is in capturing this "array" and correctly pass to my save function so that all content is inserted into the database.

HTML code of the form:

<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
});

Save module (back-end):

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 module "salvage" are 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 in the best way to solve this problem?

1 answer

0


I managed to solve it this way:

module.exports.salvar = function (application, req, res) {

var connection = application.config.dbConnection();
var boletimModel = new application.app.models.BoletimDAO(connection);

qtd = req.body.data_evento.length;

    for (i = 0; i < qtd - 1; i++) {
        data = req.body.data_evento[i];
        titulo = req.body.titulo_evento[i];

        var info_boletim = { data_evento: data, titulo_evento: titulo };

        boletimModel.salvarEventos(info_boletim, function (error, result) {
            if (error) {
                console.log("Erro ao inserir no banco de dados");
            }
        });
    }
    res.redirect('/');
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.