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.

3
  • It would be helpful if you also posted your code. Different body parsing modules can parse form data differently. Commented Dec 6, 2014 at 1:27
  • I am using body-parser. Commented Dec 6, 2014 at 1:47
  • The whole codebase is on github, the parsing code is around line 102 of this file. Commented Dec 6, 2014 at 1:51

1 Answer 1

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').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.