Check php unchecked checkbox

Asked

Viewed 13 times

-1

I have several dynamic checkbox that are listed as follows:

<?php foreach($configs as $config): ?>

<?php echo utf8_encode($config['description'])?><input style="margin-left:10px;" type="checkbox" name="in_active[<?php echo $config['config']?>]" <?php if($config['in_active'] == true){echo 'checked';}?>/> <?php if(isset($config['value'])){echo "<input type='number' class='form-control' name='value[".$config['config']."]' step='0.01' min='0' value='".$config['value']."'>";}?> <br>

<?php endforeach?> 

This data is sent to a controller, where I have an array called "in_active", which receives the checkbox.

$data = array();

        if(isset($_POST)){
            
            $data['in_active'] = $_POST['in_active'];
            $data['value'] = $_POST['value'];

            $updateConfig = new Configuration();
            $updateConfig->updateAllConfigs($data);

        }

This data is sent to a model, where I do a foreach to update the db.


            $in_active = $data['in_active'];
            foreach($in_active as $config => $val):
                    if($val){
                        $val = true;
                    }else{
                        $val = false;
                    }
                    $sql = $this->db->prepare("update configurations
                    set
                        in_active       = :in_active
                    where config = :config
                    ");
                    
                    $sql->bindValue(':config',$config);
                    $sql->bindValue(':in_active',$val);
                    
                    $sql->execute();

            endforeach;

But that way I can only update, when they are unchecked, when I uncheck no, I know that you realize that checkboxes only send something when they are checked. So, how do I get around this? How do I update db when unchecking chexbox?

1 answer

0

I had the following idea, since I can update who is marked, I will change everything to 0 in db, then update the ones that were marked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.