获取PHP中所有选中复选框的值并将它们放入数组中

时间:2021-09-19 13:10:08

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:

我正在创建一个CMS,其中我对页面进行了概述。我希望用户能够批量删除这些页面,因此我创建了一个表单,其中每个页面都有一个复选框,页面数据库ID为值和名称:

<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?

有人有解决方案吗?

1 个解决方案

#1


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

对所有复选框使用相同的名称。因此,在提交后,您将拥有要删除的页面ID的数组。

<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.

提交后,您将获得带有$ _POST ['delete_pages']的ID数组,其中包含您需要删除的实际页面ID。

#1


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

对所有复选框使用相同的名称。因此,在提交后,您将拥有要删除的页面ID的数组。

<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.

提交后,您将获得带有$ _POST ['delete_pages']的ID数组,其中包含您需要删除的实际页面ID。