Get checkbox values

1

I need to get more than one checkbox value, if I have more than one value, for example:

<input type="checkbox" name="combustivel[]" value="Marco">
<input type="checkbox" name="combustivel[]" value="Empres contratada">
<input type="checkbox" name="combustivel[]" value="Desconto">

If I need to mark the Marco and Discount checkbox, how can I get these two?

I've got this code on the web:

if(!empty($_POST['combustivel'])) {
  foreach($_POST['combustivel'] as $comb) {
          echo $comb;
  }
}

But the following warning appears:

  

Warning: Invalid argument supplied for foreach () ....

    
asked by anonymous 27.11.2014 / 13:21

1 answer

1

Maybe this error is happening because no option was selected, try to use is_array () at instead of empty () .

if (is_array($_POST['combustivel'])) {
    foreach($_POST['combustivel'] as $comb) {
        echo $comb;
    }
}
    
27.11.2014 / 22:08