1

I have multiple checkboxes with names of adminMeta[], such as:

<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="checkbox" name="adminMeta[name2]" value="1" />

and so on and I also have text inputs like this too with the same names.

When the data is posted, I am looping through using a foreach loop:

foreach($_POST["adminMeta"] as $a => $b) {

}

inside the loop, I add/update the record in my database depending on whether it exists already or not.

But I am having some issues with checkboxes and knowing whether they are checked or not.

I have tried using if(isset($b)) but that hasn't worked.

How can I tell inside my loop, whether a checkbox is checked or not?

3
  • Print out $_POST using echo '<pre>' . print_r($_POST, true) . '</pre>'; and you'll see what your keys and values are called. Commented Aug 11, 2017 at 10:19
  • A simple trick is to include an <input type="hidden" name="adminMeta[name1]" value="0"> before! the checkbox input. This way, you'll always have the element in $_POST being either 0 or 1. Commented Aug 11, 2017 at 10:24
  • Can you use javascript? Commented Aug 11, 2017 at 10:32

2 Answers 2

3

If a checkbox is not checked, then it is not a successful control.

If it is not a successful control, then it won't be included in the form data at all.

If it isn't in the form data, then it won't appear when you loop over the form data.

So

  • If it is in the form data, then it is checked
  • Otherwise it is not checked

Normally I'd approach this problem with something along the lines of:

$list_of_checkboxes = [ "name1", "name2" ];

Then generate the form with:

foreach ($list_of_checkboxes as $name) {
    ?>
        <label>
            <input type="checkbox" 
                   name="adminMeta[]" 
                   value="<?php echo htmlspecialchars($name); ?>">
             <?php echo htmlspecialchars($name); ?>
        </label>
    <?php
}

Then test the data with:

foreach ($list_of_checkboxes as $name) {
    if (in_array($name, $_POST['adminMeta'])) {
        # Checked
    } else {
        # Not checked
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, I see. So I would be better off deleting all records from the database and just re-adding them ?
@charlie — That is a valid approach (although you'd normally want to wrap that in a transaction for safety).
1

Another approach would be to set hidden inputs before each check with default value of 0:

<input type="hidden" name="adminMeta[name1]" value="0" />
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="hidden" name="adminMeta[name2]" value="0" />
<input type="checkbox" name="adminMeta[name2]" value="1" />

Now you will receive the data even if you don't check the checkboxes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.