0

I have an HTML form with a dynamic number of checkbox fields, all of which are encapsulated within a submit form. When the form is submitted, I want to loop through the values of each checkbox field with a PHP script. At the same time, I have to keep a certain ID associated with the checkbox field so that when I loop through each one in my script, I can use the ID to update the correct row in my database. Currently, I have:

<input checked="checked" name="attended_<?php echo($pid); ?>"

I'm just not sure how to go ahead and access all of the attended[] values from my PHP script (and keep the ID at the same time). Would I use a multidimensional array like the following?

<input checked="checked" name="attended[<?php echo($i); ?>][<?php echo($pid); ?>]; ?>"

I'd appreciate any help on this. Thanks!

1
  • 1
    possible duplicate of Php: multiple checkboxes, essentially, use attended[<?php echo $pid;?>] as a name. Commented Aug 3, 2011 at 17:33

3 Answers 3

2

lets say this is data from form

<input checked="checked" name="attended[1]; ?>"
<input checked="checked" name="attended[2]; ?>"

<input checked="checked" name="attended[3]; ?>"
<input checked="checked" name="attended[4]; ?>"

<input checked="checked" name="hello[1]; ?>"
<input checked="checked" name="hello[2]; ?>"

this is how it would look as array. not needed its just for show

// $k        =>      $v
$attended[1]='blah blah';
$attended[2]='blah blah';
$attended[3]='blah blah';
$attended[4]='blah blah';

$hello[1]='blah blah';
$hello[2]='blah blah';

science bit

foreach($attended as $k=>$v){

$sql = "UPDATE mytable SET attended = '$v', hello = '{$hello[$k]}' where id = '$k'";
$query = mysql_query($sql) or die("Cannot query the database.<br />" . mysql_error());

}

all associated data should have same pid e.g

<input checked="checked" name="attended[1]; ?>"
<input checked="checked" name="hello[1]; ?>"
Sign up to request clarification or add additional context in comments.

Comments

1

Usually when I create checkboxes I put an index in the name. This way you can loop through each checkbox in your submittion code.

<input type="checkbox" name="cbGroup[1]" value="y" />
<input type="checkbox" name="cbGroup[2]" value="y" />
<input type="checkbox" name="cbGroup[3]" value="y" />

and in your PHP

foreach($_POST['cbGroup'] as $index=>$checkbox) {}

You'll want to make sure your $_POST['cbGroup'] is set though because it won't be if the checkbox isn't checked.

Edit: Sorry, I should learn to read the question fully heh :) I use multidimensional arrays in PHP with HTML inputs all the time and I think it's the way I would go.

2 Comments

I did wind up using the multidimensional arrays to get it working. The tricky part, however, was submitting checkboxes that are NOT checked. For that, I had to add a hidden checkbox field in my HTML containing a value of "unchecked". This way, I can check for "unchecked" or "checked" in my PHP code. Thanks!
Yeah I find it tricky as well. Usually my checkboxes are flags for yes or no so when I sanitize my code I'll always do $_POST['mycheckbox'] = isset($_POST['mycheckbox']) ? 'y' : 'n'; to avoid having empty indexes :) Glad I could help!
1

Maybe you could do it with a hidden field which is sort of 'associated' with the checkbox by the name:

<input checked="checked" name="attended_<?php echo($i); ?>"
<input type="hidden" name="attended_<?php echo($i); ?>_reference" value="<?php echo($pid); ?>" />

So while processing the POST-data after a submit, you could put the references together again with some string-manipulation.

Comments

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.