How to fetch values from just the selected checkboxes in codeigniter

2

I have dynamic checkboxes, and wanted to fetch the values from those that were selected, like doing this with codigniter:

Model to go the fields and create the necessary checkboxes:

public function apoioPertendido(){      
    $query = $this->db->get('tblapoio');        
    return $query->result(); 
}

Controller to send the results of the model to the view: (there are more fields that I presented in the model)

public function proporEvento(){

            $natureza = $this->evento_model->naturezaEvento();
            $apoio = $this->evento_model->apoioPertendido();
            $espaco = $this->evento_model->espaco();
            $material = $this->evento_model->material();
            $suportgraf = $this->evento_model->suporteGrafico();
            $audiovisual = $this->evento_model->audioVisual();                  

            $data['title']              = 'Propor Evento';
            $data['naturezaEvento']     = $natureza;
            $data['apoioPertendido']    = $apoio;
            $data['espaco']             = $espaco;
            $data['material']           = $material;
            $data['suporteGraf']        = $suportgraf;
            $data['audioVisual']        = $audiovisual;

            $this->load->view('cliente/clienteheaderdash_view', $data);
            $this->load->view('cliente/clientemenu_view', $data);
            $this->load->view('cliente/clienteproporevento_view', $data);
            $this->load->view('cliente/clientefooterdash_view', $data);
        }

View the form and routine to write the existing checkboxes:

<label>Apoio Pertendido</label>
          <div class="form-group">
                 <?php foreach ($apoioPertendido as $row) { ?>
                 <label>
                  <input type="checkbox" class="flat-red" name="<?php  echo $row->tag ?>" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>
                   </label>
                   </br>
                <?php }  ?>
          </div>

This form is being sent to the phase2 function in a controller where it validates some fields and goes to the model:

public function etapa2(){
            $this->load->library('form_validation');

            $this->form_validation->set_rules('naturezaEvento', 'naturezaEvento', '|required|');
            $this->form_validation->set_rules('datainicio', 'datainicio', '|required|');
            $this->form_validation->set_rules('horainicio', 'horainicio', '|required|');
            $this->form_validation->set_rules('datafim', 'datafim', '|required|');
            $this->form_validation->set_rules('datainicio', 'datainicio', '|required|');


            if($this->form_validation->run() == FALSE){
                $this->index();
                }else{

                $this->evento_model->etapa2();
                }
        }

In this model step2 I keep the fields per post that come from the form, I think that it is here that I should do a routine to know the fields that are present as the checkboxes that are selected because they only interest me

public function etapa2(){
    $data = array(
        $apoiopertendido = apoioPertendido();

        'naturezaEvento'=>$this->input->post('naturezaEvento'),
        'datainicio'=>$this->input->post('datainicio'),
        'horainicio'=>$this->input->post('horainicio'),
        'datafim'=>$this->input->post('datafim'),
        'horafim'=>$this->input->post('horafim'),                        
        );      
}
    
asked by anonymous 05.06.2015 / 18:28

1 answer

1

In your view:

<input type="checkbox" class="flat-red" name="<?php  echo $row->tag ?>" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>

Change to

<input type="checkbox" class="flat-red" name="apoio[]" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>

The selected checkboxes will be in the variable as an array:

$apoio = $this->input->post('apoio');

I recommend using json_encode($apoio); to save array information in the database.

    
08.06.2015 / 21:26