PHP can't deal with checkboxes

I'm trying to get checkbox values and store them in my database. I was following this https://stackoverflow.com/a/29948042/13695248 answer but I always get value 1 in my database, even if it's not checked. I've checked the network tab and it says 'on' for all my checkboxes. This is how one of them look like:
<div class="form-check">
<input type="checkbox" class="form-check-input" id="front-disc-brake" name="front-disc-brake">
<label class="form-check-label" for="front-disc-brake">Front disc brake</label>
</div>
I've tried two methods:
$front_disc_brake = $_POST['front-disc-brake'] ?? 0;
and:
$front_disc_brake = isset($_POST['front-disc-brake']) ? 1 : 0;
but as I said, it's always 1, doesn't matter if I check it or not.
Answer
Solution:
You need to assign a value to the checkbox for it to work.
For example:
Change
<input type="checkbox" class="form-check-input" id="front-disc-brake" name="front-disc-brake">
To
<input type="checkbox" class="form-check-input" id="front-disc-brake" name="front-disc-brake" value="1">
Then the isset() check will work as expected.
Answer
Solution:
you must assign a value to the checkbox if you want it to work
<input type="checkbox" class="form-check-input" id="front-disc-brake" name="front-disc-brake" value="[just add value]">
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: can't write image data to path
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.