Приглашаем посетить
Сладков (sladkov.lit-info.ru)

Preselecting Check Boxes

Previous
Table of Contents
Next

Preselecting Check Boxes

if (isset($_POST['boxname']) && $_POST['boxname'] == 'yes') {
   echo 'checked="checked" ';
}


Although some websites pretend to group check boxes like radio buttons, this is technically not true. Every check box stands on its own; therefore, they all should have different names (it would not make sense to give them identical names). Then each check box can be treated individually: Check for the associated value attribute and print out the checked HTML attribute, if there is a match.

Prefilling Check Boxes (checkbox.php; excerpt)
<input type="checkbox" name="boxname" value="yes"
   <?php
  if (isset($_POST['boxname']) && $_POST['boxname']
      == 'yes') {
    echo 'checked="checked" ';
  }
?>/>I agree.

When using cookie data (if available), the code changes slightly.

Prefilling Check Boxes(checkbox-cookie.php; excerpt)
<?php
  require_once 'getFormData.inc.php';
?>
...
<input type="checkbox" name="boxname" value="yes"
   <?php
  if (getFormDataPOST('boxname') == 'yes') {
    echo 'checked="checked" ';
  }
?>/>I agree.


Previous
Table of Contents
Next