Welcome Guest, Not a member yet? Register   Sign In
How to pass data from a one form to another
#1

[eluser]Linkark07[/eluser]
Hi there.

Sorry if the title is a little confusing.

Ok, here is my problem: I have a dropdown list with names of some courses from a table. The user should select one of them and then click submit.

After that, another form appears with textboxes that must be populated with more information of the course the user selected, let's say, duration and cost.

I have been thinking this since yesterday but nothing has come to my mind as to how solve this part.

Anyone could guide me as to how obtain the name of the dropdown list, use it as a where for my select query and then with the results populate those textboxes.

Thanks in advance!
#2

[eluser]CroNiX[/eluser]
Code:
class Some_controller extends CI_Controller {

  public function method1()
  {
    //displays form with dropdown. The forms method submits to "some_controller/method2"
  }

  public function method2()
  {
    //get the submitted dropdown value from the first form
    $dropdown_value = $this->input->post('name_of_dropdown', TRUE);

    //(this would be done in a model, but to keep it short...)
    $data['form'] = $this->db
      ->select('duration, cost')
      ->where(field, $dropdown_value)
      ->get(table)
      ->result_array();

    //send the results of the query to the 2nd form
    $this->load->view(2nd form, $data);

    //in 2nd form view, you can now access $form['duration'] and $form['cost'] to populate your textboxes
  }
}
#3

[eluser]Linkark07[/eluser]
On the textbox form, if I use a static value on the model function everything works fine, and each textbox is populated. But if I try to pass the value from the dropdown then these messages appear:

Message: Undefined variable: info

Message: Missing argument 1 for Participante_model::obtenerdatosgrupo(), called in C:\xampp\htdocs\educcont\application\controllers\site.php on line 116 and defined

Here is the code:

Dropdown view
Code:
<?=form_open(base_url().'site/buscarcurso', $attributes)?>
              <label>Curso</label><select class="cursos" id="cursos" name="cursos">
              </br>
            &lt;?php

            foreach($grupos as $row)
            {
              echo '<option value="'.$row->nombre.'">'.$row->nombre.'</option>';
            }
            ?&gt;
            </select><p></p>

              &lt;input type="submit" name="submit" value="Confirmar Datos" /&gt;
            
               &lt;?=form_close()?&gt;


Controller

Code:
public function buscarcurso(){
  
                    $nombre = $this->input->post('cursos');
                    $info = array ('nombre' => $nombre);
                 //   $this->load->view("prueba", $info);
                  
             $buscanombre = $this->participante_model->obtenerdatosgrupo($info);
                    $this->confirma_inscr()
       }

Model
Code:
public function obtenerdatosgrupo($info){
      
   $nombre = $info['nombre'];
       $this->db->select('nombre, Horas, Costo');
       $this->db->where('nombre', $nombre);
$query = $this->db->get('curso');
        
        
        if($query->num_rows()== 1){
            
            return $query->result();
            
        } // else {
      //     redirect(base_url().'site/inscribir','refresh');
       // }

        
    }



#4

[eluser]CroNiX[/eluser]
You have a lot of unneeded stuff in there, try simplifying it to the basics.

Code:
public function buscarcurso(){
  $nombre = $this->input->post('cursos');
  $buscanombre = $this->participante_model->obtenerdatosgrupo($nombre);
}

Code:
public function obtenerdatosgrupo($nombre){
       $this->db->select('nombre, Horas, Costo');
       $this->db->where('nombre', $nombre);
       $query = $this->db->get('curso');
}
#5

[eluser]Linkark07[/eluser]
Found the last part of the issue: the function on controller for load the second form. Here is how I have it:

Code:
public function confirma_inscr(){
$data['titulo'] = 'Confirmar Inscripción';
$data['query'] = $this->participante_model->obtenerdatosgrupo($nombre);
  $this->load->view('confirmarcurso_view', $data);  
}

The message that appears when I try to load that page is: Message: Undefined variable: nombre

#6

[eluser]CroNiX[/eluser]
Because of the way you assign it in the controller, in the view it would be $query['nombre']

When you pass an array to the view, it runs extract() on the array.
#7

[eluser]Linkark07[/eluser]
Problem still persisted. But I found a solution to the problem. Dunno if it is right though, but the textboxes are populated on the second form.

In buscarcurso I added this:

Code:
$this->session->set_flashdata('nombre', $nombre);
                redirect(base_url().'site/confirma_inscr');

And in confirma_inscr I added this:
Code:
$nombre = $this->session->flashdata('nombre');


Thanks for everything CroNiX
#8

[eluser]CroNiX[/eluser]
I'd urge you to try to find the problem and correct it. Using flashdata is not a good long term solution, especially with critical data, and you can run into problems with session. If you want further help repost all of the code, along with the view for the 2nd form.

I missed where you were using $query->result(), and I told you how to do it with an array ($query->result_array()) instead of an object. So instead of $query[‘nombre’] it should have been $query->nombre in the view.
#9

[eluser]Linkark07[/eluser]
Was suspecting that wasn't the real solution. Found another way:

At the end of buscarcurso I added this:

Code:
$this->confirma_inscr($nombre);
// Calling the function
  public function confirma_inscr($nombre){

The textboxes are populated on the second form.
#10

[eluser]CroNiX[/eluser]
Good deal Smile




Theme © iAndrew 2016 - Forum software by © MyBB