I have the following checkbox created by a loop:
<input type='checkbox' value=0 name='<?php echo $question_id; ?>' />
Each checkbox will have a different ID, and when the user checks several checkboxes, I would like to gather all the id's and insert it into a table that relates an 'event' table with a 'questions' table.
However I am not getting the ids...I know I am supposed to use an array, but is that in the name attribute or the values attribute? Also, how do I pass on the IDs...does that go into the values field?
In HTML:
<input type='checkbox' value=1 name='checkboxname[<?php echo $question_id; ?>]' />
In PHP:
$set_checkboxes = array_keys($_POST['checkboxname']);
Or, alternatively:
In HTML:
<input type='checkbox' value="<?php echo $question_id; ?>" name='checkboxname[]' />
In PHP:
$set_checkboxes = $_POST['checkboxname'];
I believe you need something like ..
<input type='checkbox' value=<SOME UNIQUE VALUE> name='field_name[]' />
You'll loop through the field_name
which happens to be an array.
Try changing your input to this.
<input type='checkbox' value='<?php echo $question_id; ?>' name='questions[]' />
Now when you get the post with php it will return an array with all the question ids
$questionIds = $_POST['questions'];
Your html:
<form method="post" action="">
<input type="checkbox" id="things[]" value="red"> Cat<br>
<input type="checkbox" id="things[]" value="blue"> Mouse<br>
<input type="checkbox" id="things[]" value="green"> Car<br>
<input type="checkbox" id="things[]" value="yellow"> DaniWeb
</form>
Your PHP:
<?php
$things = serialize($_POST['things']);
$query = "INSERT INTO table(things) values($things)";
mysql_query($query) or die(mysql_error());
?>