1

If i have a form input like this :

home.ejs

<form action="/route" method="post>
<% for(var i=0; i<5; i++){ %>
<input type="checkbox" name="mytext">
<% }; %>
<input type="submit" value="submit" name="submit">
</form>

router.js

router.post('/route',function (req, res) {
var data = req.body.checkbox;
console.log(data);
});

If i check the [0], [1], [3] :

The Result: [ 'on', 'on', 'on' ]

Expectation: [ 'on', 'on', ' ', 'on', ' ']

Is there any explanation why the result only 3 not 5? and how to fix it? I want to get all the value (checked or not checked).

Thank you

Erik A
  • 31,639
  • 12
  • 42
  • 67
J.P
  • 21
  • 3
  • Without javascript ? – Evya Mar 03 '18 at 20:24
  • try req.body.mytext – zb22 Mar 03 '18 at 20:28
  • 1
    That is the normal behavior of submitting a form with checkboxes. The response will only have those that were checked. – Mikey Mar 03 '18 at 20:52
  • Possible duplicate of [Post the checkboxes that are unchecked](https://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked) – Mikey Mar 03 '18 at 20:55
  • yes, with out javascript. directly send to route, and get all the data using req.body @EvyatarMeged – J.P Mar 04 '18 at 04:00
  • still same, the result still [ 'on', 'on', 'on' ]. i can't identify which id already checked on the form @zb22 – J.P Mar 04 '18 at 04:01
  • did i must to use jquery to get all data and the list form before send to server or just send directly form data to server ? @Mikey – J.P Mar 04 '18 at 04:03

0 Answers0