user2568495
user2568495

Reputation:

Getting values of all checked checkboxes in PHP and put them in an array

I'm creating a CMS in which I have an overview of pages. I want the user to be able to mass delete these pages and so I have created a form in which each page has a checkbox with the pages database ID as value and name:

<input class="mass-delete-check" type="checkbox" name="<?=$page["id"]?>" value="<?=$page["id"]?>" id="<?=$page["id"]?>">

Now when I submit this form I need to get the values of the checkboxes that are actually checked and put them in an array I can go through to delete them. The thing here is that I will have to get checkbox values based on if they are checked and not on their name because I can't know all names.

Does anyone have a solution to this?

Upvotes: 1

Views: 429

Answers (1)

Rene Korss
Rene Korss

Reputation: 5484

Use the same name for all checkboxes. So after submiting you will have array with page IDs to delete.

<input class="mass-delete-check" type="checkbox" name="delete_pages[]" value="<?=$page["id"]?>" id="<?=$page["id"]?>">

After submit you would get array of IDs with $_POST['delete_pages'], which contains actual page IDs what you need to delete.

Upvotes: 2

Related Questions