I have a few radio buttons a user can check to add search parameters to their search query. When I make the post request to my server, I am getting 'on' as the value.
Here's a snippet of the HTML:
<form method="post" action="/">
<input type="radio" name="sushi">
<label for="sushi">Sushi</label>
<button type="submit">Enter</button>
</form>
server.js:
app.post('/', (req, res) => {
const request = req.body;
let sushi = request.sushi;
console.log(sushi); //returns 'on'
});
I can get the value from a text input field using this method with no problem, but the radio buttons do not work this way. They only return a value of on
if the radios are clicked on submit. I know I am missing something, but I'm having trouble finding any solutions that don't relate to the frontend and JQuery.
How can I pass the value of these checked buttons to my backend?
I have a few radio buttons a user can check to add search parameters to their search query. When I make the post request to my server, I am getting 'on' as the value.
Here's a snippet of the HTML:
<form method="post" action="/">
<input type="radio" name="sushi">
<label for="sushi">Sushi</label>
<button type="submit">Enter</button>
</form>
server.js:
app.post('/', (req, res) => {
const request = req.body;
let sushi = request.sushi;
console.log(sushi); //returns 'on'
});
I can get the value from a text input field using this method with no problem, but the radio buttons do not work this way. They only return a value of on
if the radios are clicked on submit. I know I am missing something, but I'm having trouble finding any solutions that don't relate to the frontend and JQuery.
How can I pass the value of these checked buttons to my backend?
Share Improve this question asked Feb 25, 2018 at 20:39 maison.mmaison.m 8734 gold badges21 silver badges40 bronze badges1 Answer
Reset to default 5Add a value property on your radio input:
<form method="post" action="/">
<input type="radio" value="sushi" name="sushi">
<label for="sushi">Sushi</label>
<button type="submit">Enter</button>
</form>
this is what gets posted