How to fetch values only from the checkbox selected in codeigniter

Asked

Viewed 757 times

2

I have dynamic checkbox, and wanted to fetch the values of the ones that are selected, how to do this with codigniter:

Model to go to the fields and create the required checkbox:

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

Controller to send the results of the model to the view: (has 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);
        }

Form view and routine to write 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 Fase2 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 Etapa2 I save the fields per post that come from the form, I think this is where I should do a routine to know the fields that are present as checkboxes that are selected because only these 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'),                        
        );      
}

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 checkbox will be in the variable as an array:

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

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

  • thanks, however I do not want to write in the database not yet, I just want to write the values to later the user confirm what he chose, however I am unable to write what comes from the array, alias I think it is coming null :/

Browser other questions tagged

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