0

Dynamically created checkbox list then POST retrieval on another page.

First off, Thanks in advance.

A couple questions regarding best practices with the checkboxes in a form and then $_POST.

Family picture album stuff. I am getting an array back from my brothers database and putting it in a loop.

Question 1- Is it best practices when wanting to retrieve this information on another page to have the "name" be the familyPictureID or should I put that in the "value"?

Sample of table element inside a loop:

<td data-label="Active">
<input class="checky" type="checkbox" name="cList[<?php echo $row['familyPictureID']; ?>]"
<?php if($row['active']==1) { echo 'value="1" CHECKED>';
} else { echo 'value="0">'; } 
?>
</td>

Question 2- When retrieving this information on a secondary page, how do I get all the values from the checkboxes? I want to know if they are checked or not but also pair that up with the 'familyPictureID'. Is that correct and best practices?

I am trying to get them like so:

if(!empty($_POST['cList'])) {
echo "check box test <pre>";
print_r($_POST['cList']);
echo "</pre>";

foreach($_POST['cList'] as $familyPictureID) {
echo "check=$familyPictureID<br>";
}
}

I would then send this back to my brothers database with an UPDATE to these family pictures whether they are checked or not.

Does this make sense?

Thanks in advance. I searched for something similar but people on here are doing some complicated stuff that seems way beyond the scope of what I am trying to do.

Thanks! -MT

1 Answer 1

1

You can do it either way. If you put the ID in the name, then you can write:

foreach (array_keys($_POST['cList']) AS $id) {
    echo "check=$id<br>";
}

If you put the ID in the value, you would write:

foreach ($_POST['cList'] AS $id) {
    echo "check=$id<br>";
}

Only boxes that are checked get sent to the server, so both of these will iterate over the checked boxes.

If you have other input fields that are also arrays based on the ID, then for consistency I would recommend doing the same thing with the checkboxes.

Sign up to request clarification or add additional context in comments.

1 Comment

Barmar, it worked!!!! Thanks man. This is cool. I will see where I can go from here and let you guys know. I am looking into UPDATES now from the ones that have been checked. Wish me luck!

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.