Forum Moderators: coopster

Message Too Old, No Replies

limit number of checkboxes

         

bartvi

12:30 pm on Sep 20, 2009 (gmt 0)

10+ Year Member



within a php-form I have these checkboxes (I display only 2 but there are 10) :
<tr><td>
<input class="inputc2" size="25" name="naam1" value="<?php echo "$_POST[naam1]"; ?>">
<INPUT TYPE="checkbox" NAME="act1[]" id="act1" VALUE="4x4">4x4
<INPUT TYPE="checkbox" NAME="act1[]" id="act1" VALUE="carcontrol">CarControl
<INPUT TYPE="checkbox" NAME="act1[]" id="act1" VALUE="karting">Karting
</td>
</tr>
<tr>
<td align="right" class="inputc1">Naam persoon 2 : </td>
<td class="inputc1">
<input class="inputc2" size="25" name="naam2" value="<?php echo "$_POST[naam2]"; ?>">
<INPUT TYPE="checkbox" NAME="act2[]" VALUE="4x4">4x4
<INPUT TYPE="checkbox" NAME="act2[]" VALUE="carcontrol">CarControl
<INPUT TYPE="checkbox" NAME="act2[]" VALUE="karting">Karting
</td>
</tr>

For each subscriber you can fill in his name and select the activity he wants to participate.
I want to know how you can limit these checkboxes to max 2 checks and display imediatly a errormessage when the third checkbox is checked.
Does anyone knows how to do this?

barns101

2:36 pm on Sep 20, 2009 (gmt 0)

10+ Year Member



For an instant error message you'd need to use JavaScript.

To ensure that no more than 2 boxes were checked server-side you could count the number of array items in $_POST['act1'] or check if isset($_POST['act1'][2]) -- remember that the array numbering starts at 0.