Currently, I am working on the task of parsing checkbox values into an array using express/ejs.
Users are required to fill out a form and select checkboxes as shown below: Answer: Checkbox:
The goal is to create two arrays from the input data:
answer = ["string 1", "string 2", "string 3", "string 4"] checkbox = [checked, unchecked, unchecked, unchecked]
The issue is that express only processes checked checkboxes and ignores unchecked ones (resulting in checkbox = "checked" only).
How can I make express log the "unchecked" values too? I have thought about setting the value to true/false depending on whether the checkbox is checked, but I am unsure how to handle this in ejs.
Thank you!
JS
router.post(`/`, (req, res) => {
console.log(req.body);
res.redirect(`/`);
})
EJS
<form action="/question" method="post">
<label for="sessionName">Session Name</label><input type="text" name="sessionName">
<div class="question">
<label for="question">Question: </label><input type="textarea" name="question">
<ul>
<li><label for="answer">Answer 1: </label><input type="textarea" name="answer"><input type="checkbox" name="test" value="1"></li>
<li><label for="answer">Answer 2: </label><input type="textarea" name="answer"><input type="checkbox" name="test" value="1"></li>
<li><label for="answer">Answer 3: </label><input type="textarea" name="answer"><input type="checkbox" name="test" value="1"></li>
<li><label for="answer">Answer 4: </label><input type="textarea" name="answer"><input type="checkbox" name="test" value="1"></li>
</ul>
</div>
<input type="submit" name="" value="Create Session">
</form>