PHP

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

<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 with Cookie

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

by BrainBellupdated
Advertisement: