Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Express Jade - Checkbox boolean value

I'm using Node+Express+Jade to render some webpages. On a form there are 2 checkboxes. When the form is submitted through POST, if the checkbox is checked, I get req.body.checkbox1 -> 'on', if isn't checked, I get req.body.checkbox1 -> undefined

Is possible to get checkbox value as true or false ?

Here's my server side test code

var bodyParser = require('body-parser');
var express = require('express');

var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/webroot'));
app.set('views', __dirname + '/view');
app.set('view engine', 'jade');
app.listen(3001);
app.get('/', function (req, res) {
    res.render('test');
});

app.post('/config', function (req, res) {
    console.log(req.body.t);
    console.log(req.body.f);
    res.redirect('/');
});

And my Jade form test

form(action='/config', method='post')
    input(type="checkbox", name="not_checked", checked=false)
    input(type="checkbox", name="checked", checked=true)
    input(type='submit')
like image 300
Matheus Vellone Avatar asked Aug 25 '15 11:08

Matheus Vellone


3 Answers

I was working through this recently for a project...

Here is the form :

form(action='/survey/submission/test/config', method='post')
    input(type="checkbox", name="1")
    input(type="checkbox", name="2")
    input(type="checkbox", name="3")
    input(type="submit")

then in the node javascript they can be found through the req.body.name of each. req.body.name will return 'on' if one of the boxes if checked. if a box is not checked, they are undefined. so a ternary statement can be used to create a new object and save them to a db as a boolean value.

object = {
    first: req.body.1 ? true : false,
    second: req.body.2 ? true : false,
    third: req.body.3 ? true : false
};

Hope this helps anyone else coming along. I was happy once I figured it out

like image 161
egervais7 Avatar answered Nov 19 '22 13:11

egervais7


I have meet this problem today and I have a solution more clean (for me):

function(req, res){
   req.body.field = Boolean(req.body.field)
}
like image 44
A. Moynet Avatar answered Nov 19 '22 13:11

A. Moynet


Can you try the following jade form

form(action='/survey/submission/test/config', method='post')
  input(type="checkbox", name="not_checked", value="false")
  input(type="checkbox", name="checked", checked, value="true")
  input(type='submit')

The value will be string, so you have to parse it. If the checkbox does not have checked attribute then it would not be included in the post request body. So in the above form, only the checked checkbox will be sent see below.

req.body : {"checked":"true"}

If you tick both checkboxes then both values will be sent as below

req.body : {"not_checked":"false","checked":"true"}

You can also add validation against undefined, whenever no checkbox is ticked

   if(req.body.not_checked) {
      console.log('not checked : ' + req.body.not_checked);
    }

    if(req.body.checked) {
      console.log('checked : ' + req.body.checked);
    }
like image 3
Raf Avatar answered Nov 19 '22 12:11

Raf