Categories
PHP

Preselecting Check Boxes

Let’s see how to return checked (or selected) checkboxes upon submitting a form. You’ll learn how to process individual or group (array) checkboxes with PHP.

A checkbox is created using the <input> element whose type attribute has a value of checkbox:

<input type="checkbox" name="tac">

The checked attribute allows a checkbox (or radio button) to be selected when the page loads:

<input type="checkbox" name="tac" checked>

Note: You can use checked, checked="", or checked="checked" all are equivalent:

<input type="checkbox" name="tac" checked>
or
<input type="checkbox" name="tac" checked="">
or
<input type="checkbox" name="tac" checked="checked">

PHP – Prefilling Check Boxes

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

You can group checkboxes like radio buttons, but it is recommended that every checkbox stands on its own; therefore, they all should have different 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.

1. Use checkboxes when you need to allow a user to provide a simple yes or no response:

<p>Terms and Conditions... ....</p>
<form method="post" action="example.php">
 <label>

  <input type="checkbox" name="tac"
   <?= isset($_POST['tac']) ? 'checked' : '' ?>
  >

Yes, I accept ...
 </label>
 <input type="submit" name="submit" value="submit">
</form>

2. Use checkboxes when you need to allow a user to select multiple options from a list:

<p>Choose tutorials:</p>
<form method="post" action="example.php">
 <label>
  <input type="checkbox" name="PHP"
   <?= isset($_POST['PHP']) ? 'checked' : '' ?>
  >PHP 
 </label>
 <label>
  <input type="checkbox" name="JS"
   <?= isset($_POST['JS']) ? 'checked' : '' ?>
  >JavaScript  
 </label>
 <label>
  <input type="checkbox" name="MYSQL"
   <?= isset($_POST['MYSQL']) ? 'checked' : '' ?>
  >MySQL 
 </label>
 <input type="submit" name="submit" value="submit">
</form>

Group checkboxes

As we already mentioned that you can group checkboxes like radio buttons, but we do not recommend it.

To group checkboxes just use the same name for all checkboxes and append the blank square brackets [] (array initialization) at the end of each checkbox name, for example, tutorials[]. You also must provide a unique value for each checkbox, for example:

<input type="checkbox" name="tutorials[]" value="PHP">PHP
<input type="checkbox" name="tutorials[]" value="JS">JavaScript
<input type="checkbox" name="tutorials[]" value="MySQL">MySQL

Submitting checkbox array to PHP:

Example 1:

<?php
 // Prior to PHP 7.0.0
 $ckb = array(); //initialize a blank array

 // true, if any checkbox is selected in the group
 if (isset($_POST['tutorials'])) {
  $ckb = $_POST['tutorials']; // pass to $ckb
 }

 //Prints values of selected checkboxes 
 foreach ($ckb as $v) {
  echo htmlspecialchars( $v ) . '<br>';
 }

Example 2:

<?php
 //For PHP 7.0.0 or above
 $ckb = $_POST['tutorials'] ?? [];
 foreach ($ckb as $v) {
  echo htmlspecialchars($v) . '<br>';
 }

PHP – preselecting group checkboxes

<?php
 $ckb = $_POST['tutorials'] ?? [];
?>
<p>Choose tutorials:</p>
<form method="post" action="example.php">
 <label>
  <input type="checkbox" name="tutorials[]" value="PHP"
   <?=in_array('PHP',$ckb) ? 'checked' : ''?>>PHP 
 </label>
 <label>
  <input type="checkbox" name="tutorials[]" value="JS"
   <?=in_array('JS',$ckb) ? 'checked' : ''?>>JavaScript  
 </label>
 <label>
  <input type="checkbox" name="tutorials[]" value="MySQL"
   <?=in_array('MySQL',$ckb) ? 'checked' : ''?>>MySQL 
 </label>
 <input type="submit" name="submit" value="submit">
</form>

We stored the submitted checkboxes in $ckb array and used the in_array function on each checkbox in the form to determine if it exists in the array, and if found, the checked attribute is applied to that checkbox.


Processing Forms in PHP: