How to bring the typed data and store in a json to do the treatment after?

2

I would like to get the data typed in my form and put it in a json, but I do not know how to do it. My HTML form looks like this:

<form action="" class="formContent" id="myform">
  <div class="row form-input" id="form">
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="nome">Nome
                        <input type="name" id="nome" name="nome" placeholder="Digite seu nome" title="">
                      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="email">Email
                        <input type="email" id="email" name="email" placeholder="Digite seu email">
                      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="telefone">Telefone
                        <input type="text" id="telefone" name="telefone" placeholder="Digite seu telefone">
                      </label>
    </div>
  </div>
  <div class="row form-Msg" id="form_two">
    <div class="col-lg-12 col-xs-12" style="padding: 0;">
      <label for="">Mensagem
                        <textarea id="mensagem" name="mensagem" rows="5" placeholder="Digite sua mensagem"></textarea>
                      </label>
    </div>
    <div class="col-lg-2">
      <input type="submit" id="enviar" name="enviar" onclick="salvar();" value="Enviar">
      <!-- <button href="#" class="FormSing" type="button" name="button">Enviar</button> -->
    </div>
  </div>
  <!-- <button class="FormSing" type="button" name="button">Enviar</button> -->
</form>
    
asked by anonymous 20.02.2017 / 15:14

2 answers

3

You can serialize your form with the help of FormData.

var serialize = function (form) {
  var json = {};
  var data = new FormData(form);
  var keys = data.keys();
  for (var key = keys.next(); !key.done; key = keys.next()) {
    var values = data.getAll(key.value);
    json[key.value] = values.length == 1 ? values[0] : values;
  }  
  return json;
}

var form = document.querySelector("form");
var enviar = document.getElementById("enviar");
enviar.addEventListener("click", function (event) {
  event.preventDefault();
  var json = serialize(form);	
  console.log(json);
});
<form action="" class="formContent" id="myform">
  <div class="row form-input" id="form">
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="nome">Nome
        <input type="name" id="nome" name="nome" placeholder="Digite seu nome" title="" value="a">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="email">Email
        <input type="email" id="email" name="email" placeholder="Digite seu email" value="[email protected]">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="telefone">Telefone 1
        <input type="text" id="telefone1" name="telefones" placeholder="Digite seu telefone" value="123456">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="telefone">Telefone 2
        <input type="text" id="telefone2" name="telefones" placeholder="Digite seu telefone" value="654321">
      </label>
    </div>
  </div>
  <div class="row form-Msg" id="form_two">
    <div class="col-lg-12 col-xs-12" style="padding: 0;">
      <label for="">Mensagem
        <textarea id="mensagem" name="mensagem" rows="5" placeholder="Digite sua mensagem">abcdef</textarea>
      </label>
    </div>
    <div class="col-lg-2">
      <input type="submit" id="enviar" name="enviar" value="Enviar">
    </div>
  </div>
</form>

Now a version with jQuery.:

var serialize = function (form) {
  var data = form.serializeArray();
  var json = {};
  data.forEach(function (item) {
    if (!json[item.name]) {
      json[item.name] = item.value;
      return;
    }    
    if (!Array.isArray(json[item.name]))
      json[item.name] = [json[item.name]];
    json[item.name].push(item.value);
  });
  return json;
}

var form = $("form");
var enviar = $("#enviar");
enviar.on("click", function (event) {
  event.preventDefault();
  var json = serialize(form);	
  console.log(json);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formaction="" class="formContent" id="myform">
  <div class="row form-input" id="form">
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="nome">Nome
        <input type="name" id="nome" name="nome" placeholder="Digite seu nome" title="" value="a">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="email">Email
        <input type="email" id="email" name="email" placeholder="Digite seu email" value="[email protected]">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="telefone">Telefone 1
        <input type="text" id="telefone1" name="telefones" placeholder="Digite seu telefone" value="123456">
      </label>
    </div>
    <div class="col-lg-4 col-sm-4 col-xs-12">
      <label for="telefone">Telefone 2
        <input type="text" id="telefone2" name="telefones" placeholder="Digite seu telefone" value="654321">
      </label>
    </div>
  </div>
  <div class="row form-Msg" id="form_two">
    <div class="col-lg-12 col-xs-12" style="padding: 0;">
      <label for="">Mensagem
        <textarea id="mensagem" name="mensagem" rows="5" placeholder="Digite sua mensagem">abcdef</textarea>
      </label>
    </div>
    <div class="col-lg-2">
      <input type="submit" id="enviar" name="enviar" value="Enviar">
    </div>
  </div>
</form>
    
20.02.2017 / 16:01
0

You can do this easily with JQuery using this function:

var formdata = $("#meu_formulario").serialize();
    
20.02.2017 / 15:35