[PHP Code > PHP Checkbox Control]

Checkboxes

Picture of me and my dog

This demo shows the use of the HTML Checkbox Control and how to use a PHP array to gather the user's input after a Submit. This also illustrates how to maintain and show what the user selected -- in other words, make the form sticky.

Apple
Pear
Orange
Mango
Apricot


You may use the following script as you need without any obligation to me whatsoever. However, a Donation via PayPal would be greatly appreciated -- please note, this site does not accept advertising.


Here's the PHP/HTML code for the "Checkbox" Control:

00 
07 <?php 08 09 error_reporting(E_ALL); // set error reporting to all 10 $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); 11 12 $option = array(); 13 $s = ' CHECKED '; 14 15 if(isset($_POST['option'])) 16 { 17 foreach ($_POST['option'] as $value) 18 { 19 $option[] = $value; 20 } 21 } 22 ?> 23 24 24 <!doctype html> 24 <html lang="en-us"> 24 24 <head> 24 <meta charset="utf-8"> 24 <title>tedd's Examples</title> 24 </head> 24 24 <body> 25 40 <form action="<?php echo($self); ?>" method="POST" class="examples"> 41 <p> 42 <input type="checkbox" name="option[]" value="Apple" <?php if (in_array('Apple', $option)) echo($s); ?>> Apple<br> 43 <input type="checkbox" name="option[]" value="Pear" <?php if (in_array('Pear', $option)) echo($s); ?>> Pear<br> 44 <input type="checkbox" name="option[]" value="Orange" <?php if (in_array('Orange', $option)) echo($s); ?>> Orange<br> 45 <input type="checkbox" name="option[]" value="Mango" <?php if (in_array('Mango', $option)) echo($s); ?>> Mango<br> 46 <input type="checkbox" name="option[]" value="Apricot" <?php if (in_array('Apricot', $option)) echo($s); ?>> Apricot<br> 47 </p> 48 <p> 49 <input type="submit" name="submit" value="Submit"> 50 </p> 51 </form> 52 52 </body> 52 </html>
  Show the above CODE for Copy/Paste below.

The following code will work "as-is" without any additional coding. However, remember to use a .php' suffix.


<?php error_reporting(E_ALL); // set error reporting to all $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); $option = array(); $s = ' CHECKED '; if(isset($_POST['option'])) { foreach ($_POST['option'] as $value) { $option[] = $value; } } ?> <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <title>tedd's Examples</title> </head> <body> <form action="<?php echo($self); ?>" method="POST" class="examples"> <p> <input type="checkbox" name="option[]" value="Apple" <?php if (in_array('Apple', $option)) echo($s); ?>> Apple<br> <input type="checkbox" name="option[]" value="Pear" <?php if (in_array('Pear', $option)) echo($s); ?>> Pear<br> <input type="checkbox" name="option[]" value="Orange" <?php if (in_array('Orange', $option)) echo($s); ?>> Orange<br> <input type="checkbox" name="option[]" value="Mango" <?php if (in_array('Mango', $option)) echo($s); ?>> Mango<br> <input type="checkbox" name="option[]" value="Apricot" <?php if (in_array('Apricot', $option)) echo($s); ?>> Apricot<br> </p> <p> <input type="submit" name="submit" value="Submit"> </p> </form> </body> </html>