Codeigniter get values of an input or a variable from the controller

1

I'm new to codeigniter and I wanted to know if you can get the value of an input tag or a php variable from the controller. What I want to achieve is to send data from one visa to another in a similar way to the POST method (without the values being visible in the URL)

For example:

View 1:

<html>
  <body>
   <input type="number" class="form-control" id="valueInput">
   <?php $valueVaraible = "Ejemplo" ?>
   <a type="button" href="<?php echo base_url()?>index.php/Welcome/toView2">Redirigir a vista 2</a>
  </body>
</html>

Controller:

public function toView2() {
    $this->load->helper('array');
    $this->load->helper('url');

    $arrayData = array(
        'valueInput' => "algun metodo para otener el valor del input", 
        'valueVaraible'=>"algun metodo para otener el valor de la variable");

    $this->load->view('view2', $arrayData);
}
    
asked by Alberto Ugalde 11.09.2017 в 21:25
source

2 answers

0

What you have to do is a form to send the data you want by POST and in your controller to receive it with the support of the class Input . As I show in the following code:

View:

<form action="<?php echo base_url()?>index.php/Welcome/toView2" method="POST">
    <input type="number" class="form-control" id="valueInput" name="valueInput" value="Algún valor">
    <?php $valueVaraible = "Ejemplo" ?>
    <!--Enviar otros valores que no se muestran en el formulario -->
    <input type="hidden" id="valueInput" name="valueInput" value="<?php echo $valueVaraible ?>">
</form>

Controller:

public function toView2() {
    $this->load->helper('array');
    $this->load->helper('url');

    $arrayData = array(
        'valueInput' => $this->input->post('valueInput'), 
        'valueVaraible'=>$this->input->post('valueVaraible'));

    $this->load->view('view2', $arrayData);
}

Here you have the official documentation .

    
answered by 11.09.2017 / 22:07
source
0

Better use the codeigniter helper

In your view you will have:

<?php echo form_open("controlador/metodo"); ?> 
  <input placeholder="txtNombre" type="text" name="nombre">
  <input placeholder="txtApaterno" type="text" name="apellido">
  <input placeholder="txtAmaterno" type="text" name="apellido">
  <button type="submit">Login</button>
<?php echo form_close(); ?>

<?php
$msj = (isset($msj)) ? $msj:validation_errors();
if (strlen($msj) > 0 )
    echo "<div class='alert alert-info'><h1 class='text-center'> <a href='".site_url("cliente")."'>Atras</a> |  $msj  </h1></div>";

?>

and in your controller you will have:

<?php

public function index(){
  //Se cargan los helpers y librerias de codeigniter
  //que nos facilitan algunas cosas.
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
}

public function frm_alta()
{
  //Cliente es mi controlador, y frm_alta es mi vista frm_alta.php
  $this->load->view("Cliente/frm_alta");
}

public function agregar()
{
  //validacion de los campos, puedes leer sobre esto aqui:
  //https://www.codeigniter.com/userguide3/libraries/form_validation.html

  $this->form_validation->set_rules("txtNombre", "Nombre", "trim|required");
  $this->form_validation->set_rules("txtApaterno", "Apaterno", "trim");
  $this->form_validation->set_rules("txtAmaterno", "Amaterno", "trim");
  if ($this->form_validation->run() == true) {

      $this->load->model("Modelocliente");

      $insert = $this->Modelocliente->agregar(
          $this->input->post("txtNombre"),
          $this->input->post("txtApaterno"),
          $this->input->post("txtAmaterno")
      );
      if ($insert > 0) {
          $this->data["msj"] = "Cliente agregado";
          $b = 1;
      } else {
          $this->data["msj"] = "Error al realizar el proceso";
      }
  } else {
      $this->frm_alta();
  }

  } //FIN DE LA FUNCION

?>

And lastly in your Model you should have:

function agregar($nombre,$apaterno,$amaterno)
{
  $array = array("idCliente"=>0,
                  "Nombre"=>$nombre,
                  "Apaterno"=>$apaterno,
                  "Amaterno"=>$amaterno
              );
  $this->db->insert("Cliente",$array);
  return $this->db->affected_rows();
}

I hope you serve my example, I had it more complex so to speak but simplify it so you understand well how Codeigniter works, I recommend you to read the manual :) It is not as complicated as other frameworks

    
answered by 22.05.2018 в 19:56