lundi 30 novembre 2015

A 'check all' checkbox (JavaScript) in a PHP 'while loop'

I'm looking for a way to use JavaScript in the following piece of code. I want the checkbox with id = alles to check all the available checkboxes that are printed during the while loop.

<form id="andere" name="andere" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
      <fieldset><legend>Selecteer de klassen waaraan je de persoon wilt koppelen:</legend>
        <table>
          <tr>
            <td><input type="checkbox" id="alles" name="alles" value="">Selecteer alle klassen</td>
          </tr>
          <tr>
<?php
  $query='SELECT oc_klas.klascode, oc_klas.klas_ID FROM oc_klas ORDER BY oc_klas.klas_ID';
  $result=mysqli_query($mysqli,$query) or die('<p>Kan query: '.$query.' niet uitvoeren.</p>');
  $aantalRijen = mysqli_num_rows($result);
  $KLASSEN_PER_RIJ = 10;
//bereken de colspan van de laatste kolom
  $fill=$aantalRijen%$KLASSEN_PER_RIJ;

  $fill=$KLASSEN_PER_RIJ-$fill;
  $teller=1;

  while($myrow=mysqli_fetch_row($result))
  {
    print('<td><input type="checkbox" id="klas'.$myrow[1].'" name="klas'.$myrow[1].'" value="'.$myrow[1].'">'.$myrow[0].'</td>');
    if($teller%$KLASSEN_PER_RIJ==0)
    {
      print('</tr><tr>');
    }
    $teller++;
  }
  print('</tr></table>');
?>

Usually I rely on the onclick function:

onclick="for(c in document.getElementsByName('klas')) document.getElementsByName('klas').item(c).checked = this.checked"

but since the ID's are not identical now, I'm not sure how to tackle this particular problem. Any ideas?




Aucun commentaire:

Enregistrer un commentaire