jcadima
4/13/2015 - 4:45 AM

How to get data from an array of check boxes

How to get data from an array of check boxes

<?php 
// The HTML
?>
<input type="checkbox" name"top[]" value="pep" > Pepperoni<br>
<input type="checkbox" name"top[]" value="msh" > Mushrooms <br>
<input type="checkbox" name"top[]" value="olv" > Olives

<?php  
// The PHP that accesses the array and its values
$toppings = filter_input(INPUT_POST, 'top', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY) ;
if ( $toppings !== NULL) {
  $top1 = $toppings[0];
  $top2 = $toppigns[1];
  $top3 = $toppings[2];
}



// The Loop
$toppings = filter_input(INPUT_POST, 'top', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY) ;
if ($toppings !== NULL) {
  foreach( $toppings as $key => $value) {
    echo $key . ' = ' . $value . '<br>' ;
  }
}
else {
  echo ' no toppings selected';
}

// output:
0 = pep
1 = olv

?>