2

I am trying to create a form with several checkboxes. I want each checkbox to share the same name, and the values of the ones checked off to be submitted with the form. I know that this can be done with PHP (see this SO question) but I can't figure out how to do it with Node.js and Express. Here's a modified version of my HTML:

<label><input type="checkbox" name="section[]" value="1" />Item Name</label>
<label><input type="checkbox" name="section[]" value="2" />Item Name</label>
<label><input type="checkbox" name="section[]" value="3" />Item Name</label>

When I handle the POST request, I've tried accessing the values with both req.param('section') and req.param('section[]') but they both return undefined.

Community
  • 1
  • 1
Greg
  • 1,225
  • 3
  • 16
  • 35
  • It would be helpful if you also posted your code. Different body parsing modules can parse form data differently. – mscdex Dec 06 '14 at 01:27
  • I am using [body-parser](https://github.com/expressjs/body-parser). – Greg Dec 06 '14 at 01:47
  • The whole codebase is on github, the parsing code is around [line 102 of this file](https://github.com/gregthegeek/JGrader/blob/master/JGrader/routes/teacher.js#L102). – Greg Dec 06 '14 at 01:51

1 Answers1

8

If you change this line in your app.js:

app.use(bodyParser.urlencoded({ extended: false }));

to:

app.use(bodyParser.urlencoded({ extended: true }));

you should get what you are expecting with req.param('section').

mscdex
  • 104,356
  • 15
  • 192
  • 153