www.webdeveloper.com
Results 1 to 2 of 2

Thread: Dynamic Checkbox Validation

  1. #1
    Join Date
    Jun 2009
    Posts
    1

    Dynamic Checkbox Validation

    I'm having a problem validating whether at least one checkbox is checked in the following situation.

    I have a site that creates a checkbox for each row in a mySql database, and allows the user to check it, then hit a submit button and delete all of the checked boxes. The code that generates each row and the checkbox is here:

    Code:
          <?php while ($row_info = mysql_fetch_assoc($info)) { ?>
            <tr>
              <td align="center"><?php echo $row_info['item']; ?></td>
              <td align="center"><?php echo $row_info['first_name'];?></td>
              <td align="center"><input type='checkbox' name='checkbox[]' id='checkbox[]' value='<?php echo $row_info['id']; ?>'></td>
            </tr>
            </td>
            
            <?php  } ?>
    Any help on how I can validate whether at least one checkbox is selected would be great.

  2. #2
    Join Date
    Oct 2006
    Posts
    93
    You could give the ids of the checkboxes some standard name and append a counter to it.
    (ie Box_0, Box_1, Box_2,...., Box_N)

    Then you could do something like....

    Code:
    for (var x=0, myBox=document.getElementById('Box_' + x); myBox; x++, myBox=document.getElementById('Box_' + x))  {     //will loop through all boxes
         var isChecked = false;
         if (myBox.checked)  {     //found a checked box, so exit out of loop
              isChecked=true;
              break;
         }
         
    }
    Not sure the code is actually valid, but you get the idea.

    Or if they are the only checkboxes on the page, you could get all the checkboxes in an array and do the same thing.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles