How to do a checkbox ternary check with PHP?

0

I am doing this check in a <select> field and everything is working fine but I would like to do the same check in a <checkbox> that can have several possibilities, then need to work with arrays so I do not know how to proceed.

Below the code I use for <select> :

PHP

if( isset( $_POST['valorminimo'] ) ) $_SESSION['valorminimo'] = $_POST['valorminimo'];

HTML

<option value="1mil" <?php echo (($valormaximo == '1mil') ? 'selected="selected"' : ""); ?>>1mil</option>

Now I have the code PHP below for the checkboxes that retrieves the indexes correctly and I need to know in HTML how to do a ternary check to apply an effect on checked .

PHP

$dorms = isset( $_SESSION['dorms'] ) ? $_SESSION['dorms'] : array();

HTML

<input id="dorm1" class="hidden drm" type="checkbox" name="dorms[]" value="1" <?php ((in_array($dorms)) ? "checked='checked'" : "" ); ?>>                                   
<input id="dorm2" class="hidden drm" type="checkbox" name="dorms[]" value="2">                                  
<input id="dorm3" class="hidden drm" type="checkbox" name="dorms[]" value="3+">
  

In short, how to check in every input type checkbox whether it is checked or not?

    
asked by anonymous 02.01.2016 / 16:03

1 answer

2

Checkboxes that have not been checked will not be sent to the server, but depending on the context that this application works it is necessary to check the value of the checkbox to avoid future upheavals.

Assuming the variable name you gave var_dump is $valores

if(isset($valores['dorms']) && is_array($valores['dorms']) && !empty($valores['dorms'])) { // verificações básicas
    foreach($valores['dorms'] as $key => $dorm) { // percorre a array
        if(!empty($dorm)) { // verifica se não está vazio
            echo 'O checkbox de índice ['.$key.'] e valor ['.$dorm.'] foi marcado';
        }
    }
}

and only ...

    
02.01.2016 / 16:28