First of all I would change these
<form method="post">
<input type="hidden" name="blah" value="blah">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<button type="submit">Submit</button>
</form>
As you wont be able to tell severside which one is checked currently you’ll get something like this if only one of them is checked, in the $_POST
array(
'blah' => 'blah', //your hidden field
'cbox' => array(
0 => 1
)
);
And if 2 are checked
array(
'blah' => 'blah', //your hidden field
'cbox' => array(
0 => 1
1 => 1
)
);
But the question is which 2? So you will either need to change the names of these or the values
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="2">
<input type="checkbox" name="cbox[]" value="3">
<input type="checkbox" name="cbox[]" value="4">
<input type="checkbox" name="cbox[]" value="5">
OR
<input type="checkbox" name="cbox1" value="1">
<input type="checkbox" name="cbox2" value="1">
<input type="checkbox" name="cbox3" value="1">
<input type="checkbox" name="cbox4" value="1">
<input type="checkbox" name="cbox5" value="1">
OR if you want to further complicate things
<input type="checkbox" name="cbox[cbox1]" value="1">
<input type="checkbox" name="cbox[cbox2]" value="1">
<input type="checkbox" name="cbox[cbox3]" value="1">
<input type="checkbox" name="cbox[cbox4]" value="1">
<input type="checkbox" name="cbox[cbox5]" value="1">
Depending on what one you pick, you will need to then check if it is set, if the checkbox is not checked it wont be sent to the sever. There are a few ways around this depending how you name or do the values. The best approach is simply using
$cbox1 = isset( $_POST['cbox1'] ) ? 1 : 0; //if you rename them
///or -- ( or something similar for each if you change the value )
if( in_array( 1, $_POST['cbox'] )){
$cbox1 = 1
}else{
$cbox1 = 0;
}
Now which one you pick to do should depend on if the number of checkboxes is known, a fixed number, or dynamic. If it’s a fixed number I would change the name ( the 2nd option ) as it is cleaner and will be much easier to deal with server side.
1
solved How do I insert the value from a checkbox into MySQL in php? [duplicate]