I'm having trouble grasping the concept of using HTML checkboxes
with Node.js and Express. I have a basic form in EJS
and before diving deeper into the backend logic, I want to ensure that the correct values are being retrieved. Despite my efforts to find help elsewhere, every console.log
statement is returning false
instead of true
when the checkbox is selected. I suspect it might be an issue with my Express backend rather than the form itself. Any assistance would be greatly appreciated.
EJS
<form action="/results" method="POST">
<ul>
<li>
<label for="citysearch">City Name</label>
<input type="text" name="citysearch" id="citysearch" placeholder="e.g. Los Angeles">
</li>
<li>
<label for="Celcius">Celcius?</label>
<input type="checkbox" name="celcius" id="celcius" checked="true">
</li>
</ul>
<button type="submit">Submit</button>
</form>
Express/Node
app.post('/results', function(req, res){
var checked = req.body.checked
if (checked == "true") {
console.log("true");
} else {
console.log("false");
}
});