4

I have a form and part of it asks the user if they are male or female. Here is the html,

<form role="form" id="createAccountForm" action="CAform" method="POST">
.
.
.
.
<div class="form-group" id="gender">
<label class="radio-inline"><input type="radio" name="optradio" value="1">Male</label>
<label class="radio-inline"><input type="radio" name="optradio" value="0">Female</label>
</div>
.
.
.
</form>

and on the server (a node server with express framework), I want to know how to access the gender value when the user submits the form. Can you do it with body-parser?

1
  • 2
    Yes, what's the server side code? It will be something like req.body.optradio
    – michelem
    Jul 30, 2015 at 17:29

2 Answers 2

7

You just need to set the middlewares of bodyParser and you're good to go.

var bodyParser = require('body-parser'); 
app.use(bodyParser.json()); // to support JSON bodies
app.use(bodyParser.urlencoded({ extended: true })); // to support URL-encoded bodies

app.post('/CAForm', function(req, res) {
  res.send(req.body.optradio);
});
1
  • 2
    these have been added to Express. bodyParser is no longer recommended.
    – Someone
    Mar 4, 2021 at 18:04
0

According to your code. You can do it by using req.body.optradio and If you console log that you will get 1 or 0 as you have given those values in your value class.

1
  • 1
    Welcome to SO! Although your code snippet may solve the OP problem, there is an already accepted answer that solved the problem providing more details than yours. In this case, the chance that your answer claim any attention and be upvoted is too small. Oct 30, 2022 at 16:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.