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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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