FRECEENA FRANCIS
FRECEENA FRANCIS

Reputation: 181

Checking all checkboxes are checked using PHP

I have few checkboxes, I want to find whether all the checkboxes are checked and if yes return a message.

<label class="control-label col-md-3">L4 Deliverables</label>
    <?php 
        while($subd_row=$subd_result->fetch_assoc()){
          if($sub_row['selected'] == 1)
          {
     ?>     
     <input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name'];?>" checked><?php echo $subd_row['d_name'];?></input>
     }

Using the above code the checkboxes are displayed. The message could be for example: " 14 checkboxes are checked".

Upvotes: 1

Views: 371

Answers (2)

Akhil Manchanda
Akhil Manchanda

Reputation: 53

On the backend, you can always check the length of the variables/array passed by a form. L4d[] will have the values of checked checkboxes only. You can simply check as:

if(count($_POST['L4d']))== 14) {...}

If you want something like an alert box to be popped up when all the checkboxes are checked, then you may call a javascript function 'onChange' of your checkbox field

Upvotes: 0

Rahul
Rahul

Reputation: 18557

You can use $i to increment when it goes in that if statement it will increment,

<label class="control-label col-md-3">L4 Deliverables</label>
<?php $i = 0;
while ($subd_row = $subd_result->fetch_assoc()) {
    if ($sub_row['selected'] == 1) {
        $i++;
?>
        <input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name']; ?>" checked><?php echo $subd_row['d_name']; ?></input>
<?php
    }
}
?>
<label><?php echo ($i <= 1 ? "$i checkbox is ": "$i checkboxes are ")."checked"; ?></label>

Upvotes: 1

Related Questions