jeudi 6 février 2020

NodeJS send to POST also unchecked input

I have a form in which I iterate an input checkbox field n times, as follows:

   <form class="" action="" method="post">
            <% availableDirezione.forEach((direzione, index) => { %>
                <p>
                    <input class="checkbox" type="checkbox" name="checkArray" id="check0" value="1">           
                    <span><%= direzione.usr_udf_direzione %> (<%= direzione.usr_udf_dirid %>) </span>
                </p>
            <% }) %>
      <button type="submit" class="btn btn-primary float-right mb-5">Add Mapping</button><br>
   </form>

I would like that in the post method, even the unchecked values are passed in the checkArray object.

Let's suppose the forEach iterates 5 times, and only the first and last checkbox are checked, I would like that the checkArray object is as following:

...
body : {
 checkArray : [1,0,0,0,1]
}
...

I tried adding the hidden checkbox with a 0 value. It fills the checkArray object 5 times but in wrong error. Any tips?




Aucun commentaire:

Enregistrer un commentaire