Pass select values by POST to CodeIgniter controller

2

You will see I want to obtain the value of the options chosen in the select , through POST , do I have to do a array with the values selected ?, I tried to do something but I did not get it
View of the form

<?php if(!empty($GenerarExamen)){
                foreach ($GenerarExamen as $ge) {
                    if($ge->idTipoPreg=='1'){ ?>
        <form action="<?=base_url()?>index.php/examenEmp/Result/<?=$ge->idExamen?>/<?=$cveEmpleado?>/<?=$ge->idSolicitud?>" method="POST" class="form">
                    <fieldset>
                    <input type="hidden" name="idPregunta" value="<?=$ge->idPregunta?>">
                        <legend><?=$ge->pregunta?></legend>
                        <select name="pregunta" class="form-control" required="target"  >
                        <option value="">Selecciona una respuesta</option>
                        <?php 
                        $this->load->model('examenEmp_model');
                        $idPreg=$ge->idPregunta;
                        $Resp= $this->examenEmp_model->respuesta($idPreg);
                        if(!empty($Resp)){
                            foreach ($Resp as $t) {
                                ?>
                                    <option value="<?=$t->inciso?>"><?=$t->respuesta?></option> 
                                <?php } 
                            }?>
                            </select>
                        </fieldset>
                        <br>

                        <?php }elseif ($ge->idTipoPreg=='2') {?>
                        <fielset>
                            <input type="hidden" name="idPregunta" value="<?=$ge->idPregunta?>">
                            <br>
                            <label><?=$ge->pregunta?></label>
                            <br>
                            <textarea required="target" placeholder="Escribe tu respuesta..." class="form-control"></textarea>
                        </fieldset>
                        <?php
                    }
                }
            }

            ?>
            <button type="submit" class="btn btn-large">Aceptar</button>
        </form>

And here the code of my controller, works if I only do it with a question but I do not know how to do it with more than one. Could you help me? :)

   <?php  public function Result($idExamen,$cveEmpleado, $idSolicitud) {
     $valor=$this->input->POST('valor');
     $r=$this->input->POST('incisoCto');
     $incisoS=$this->input->POST('pregunta');
     if ($incisoS==$r) {
     $this->examenEmp_model->agregarR($valor,$idSolicitud);
     }
     else{
      echo "<script>
     alert('No se genero examen');
     </script>" ;
      }
      $this->data['results'] = $this->examenEmp_model->resultTeorico($idSolicitud);
      $this->load->view('result_display', $this->data);
      }?>
    
asked by Gaby Hdz 12.01.2017 в 18:34
source

1 answer

3

Here's the answer in case someone is interested.

I just needed to fix a small detail and create a for to get the values in the controller

public function Result($idExamen,$cveEmpleado, $idSolicitud)
 {
  $sum = 0;
 for($i=0; $i<count($this->input->post("preguntaM[]")); $i++)
 {
  $valor[$i]=$this->input->post("valor[".$i."]");
  $r[$i]=$this->input->post("incisoCto[".$i."]");
  $incisoS[$i]=$this->input->post("preguntaM[".$i."]");
  if ($incisoS[$i]==$r[$i]) {
  $sum += $valor[$i];
   }
  }

And add brackets in the view.

<div class="container">
    <div class="col-xs-12" align="center">
        <h2>Exámenes a realizar</h2>
        <?php if(!empty($GenerarExamen)){
            foreach ($GenerarExamen as $ge) {
                if($ge->idTipoPreg=='1'){ ?>
                <form action="<?=base_url()?>index.php/examenEmp/Result/<?=$ge->idExamen?>/<?=$cveEmpleado?>/<?=$ge->idSolicitud?>" method="POST" class="form">
                    <fieldset>
                        <input type="hidden" name="idPregunta" value="<?=$ge->idPregunta?>">
                        <legend><?=$ge->pregunta?></legend>
                        <input type="hidden" name="incisoCto[]" value="<?=$ge->incisoCto?>">
                        <input type="hidden" name="valor[]" value="<?=$ge->valor?>">
                        <select name="preguntaM[]" class="form-control" required="target"  >
                            <option value="">Selecciona una respuesta</option>
                            <?php 
                            $this->load->model('examenEmp_model');
                            $idPreg=$ge->idPregunta;
                            $Resp= $this->examenEmp_model->respuesta($idPreg);
                            if(!empty($Resp)){
                                foreach ($Resp as $t) {
                                    ?>
                                    <option value="<?=$t->inciso?>"><?=$t->respuesta?></option> 
                                    <?php } 
                                }?>
                            </select>
                        </fieldset>
                        <br>

                        <?php }elseif ($ge->idTipoPreg=='2') {?>
                        <fielset>
                            <input type="hidden" name="idPregunta" value="<?=$ge->idPregunta?>">
                            <br>
                            <label><?=$ge->pregunta?></label>
                            <br>
                            <textarea name="preguntaA" required="target" placeholder="Escribe tu respuesta..." class="form-control"></textarea>
                        </fieldset>
                        <?php
                    }
                }
            }

            ?>
            <button type="submit" class="btn btn-large">Aceptar</button>
        </form>
    </div>
    
answered by 12.01.2017 / 22:19
source