How to create a jquery object and insert it into mongodb via ajax?

Asked

Viewed 67 times

1

I have the following structure in html:

<form id="titulo">
 <input type="text" name="nome">
</form>
<form id="turno1">
 <div class="linha">
  <input type="text" name="dia">
  <input type="text" name="inicio">
  <input type="text" name="fim">
 </div>
 <div class="linha>
  <input type="text" name="dia">
  <input type="text" name="inicio">
  <input type="text" name="fim">
 </div>
</form>
<form id="turno2">
 <div class="linha">
  <input type="text" name="dia">
  <input type="text" name="inicio">
  <input type="text" name="fim">
 </div>
 <div class="linha>
  <input type="text" name="dia">
  <input type="text" name="inicio">
  <input type="text" name="fim">
 </div>
</form>

I can get the value of all the fields with the javascript. I can also separate the values of inputs day, beginning and end, for each of the forms. But I would like to build a structure like this:

{
"nome" : [{
  "turno1" : [{
              "dia" : [{
                        "inicio" = "valor_inicio",
                        "fim" = "valor_fim"
                      }],
              "dia" : [{
                        "inicio" = "valor_inicio",
                        "fim" = "valor_fim"
                      }],
   }],

   "turno2" : [{
               "dia" : [{
                        "inicio" = "valor_inicio",
                        "fim" = "valor_fim"
                       }],
              "dia" : [{
                        "inicio" = "valor_inicio",
                        "fim" = "valor_fim"
                      }],
   }]
}

Then send via $.ajax() and with the PHP insert into a collection in the mongodb.

How do I mount this object? I reinforce that my doubt is not as send via ajax and insert in mongodb. Just how I build this object.

After help, I was able to develop the following code:

var obj = {};
$("form").each(function (i) {
    if (i == 0) {
        form1 = $("[titulo='Nome']", $(this)).val();
        obj[form1] = [{}];
    } else {
        formulario = $(this).attr("id");
        linha = "#" + formulario + " .linha";
        $(linha).each(function () {
            obj[form1][0][formulario] = [{
                    [$("[name='dia']", $(this)).val()]: [{
                            "inicio": $("[name='inicio']", $(this)).val(),
                            "fim": $("[name='fim']", $(this)).val(),
                        }]
                }]

        });
    }
});

But it returns only the value for the first line.

  • The "day" key must be the field value name="dia" or simply the day, even string?

  • In this case the value of the field.

1 answer

0


I believe that is what you want. To make a loop for forms and create the object with the arrays by inserting the values of the fields:

var obj = {};

$("form").each(function(i){
   
   var $this = $(this);
   
   if(i == 0){

      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}];

   }else{
      
      obj[form1][0][$this.attr("id")] = [{}];

      $(".linha", $this).each(function(){
      
         obj[form1][0][$this.attr("id")][0][$("[name='dia']", $(this)).val()] = [{
      
               "inicio" : $("[name='inicio']", $(this)).val(),
               "fim" : $("[name='fim']", $(this)).val(),
         
         }]
      });
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
   <input type="text" name="nome" value="Fulano">
</form>

<form id="turno1">
   <div class="linha">
      <input type="text" name="dia" value="t1 d1">
      <input type="text" name="inicio" value="t1 ini1">
      <input type="text" name="fim" value="t1 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t1 d2">
      <input type="text" name="inicio" value="t1 ini2">
      <input type="text" name="fim" value="t1 fim2">
   </div>
</form>

<form id="turno2">
   <div class="linha">
      <input type="text" name="dia" value="t2 d1">
      <input type="text" name="inicio" value="t2 ini1">
      <input type="text" name="fim" value="t2 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t2 d2">
      <input type="text" name="inicio" value="t2 ini2">
      <input type="text" name="fim" value="t2 fim2">
   </div>
</form>

  • Cara worked perfectly! But I ended up making a mistake. Each of the forms has more than one line, and each line has the 3 fields: day, start and end. I edited my question to see if you can help me with this detail.

  • What was the mistake?

  • I changed the question by correcting my mistake.

  • ok.. I’ll take a look and update the response

  • Updated response.

  • Man, thank you so much! It worked perfectly. I will look for material to better understand arrays and objects in javascript, because I could not understand the use of [0]. But thanks for the strength!

  • When I create the object array: obj[form1] = [{}]; automatically it has an index [0]... then just add elements in [0]

  • Got it. Now it’s getting clearer. Vlw by explanation

Show 3 more comments

Browser other questions tagged

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