Jump to content

foreach loop processing checkboxes


denno020

Recommended Posts

I've got a page that has a dynamic number of checkboxes. I'm trying to figure out how to process the form once submitted. I've got a foreach loop set up, but I need to know the id of the checkbox so that I can make the change to my database accordingly.

 

At the moment, my form is built with the checkboxes given the name of "checkbox['$id']", where id is pulled from the database. In my parsing script, I've got this foreach loop that I found on the internet

 

foreach($_POST["checkbox"] as $checkbox){
}

But I don't know what to put in that foreach loop to be able to grab the id that the checkbox was given when the form was initially built?

 

I'm struggling to find any examples on the Internet :/..

Denno

Link to comment
https://forums.phpfreaks.com/topic/253397-foreach-loop-processing-checkboxes/
Share on other sites

Here is an example how you can do that..

<input type="checkbox" name="checkbox[iD]" value="ID" />
<input type="checkbox" name="checkbox[135]" value="135" />

<?php 
$checkboxes = !empty($_POST['checkbox'])? $_POST['checkbox'] : array();
foreach ($checkboxes as $id => $value)
{
// code goes here...
}
?>

Let me know if this helps.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.