Welcome Guest, Not a member yet? Register   Sign In
How to (correcly) pass variables from a loop (in view) to a Controller
#1

[eluser]Juan Velandia[/eluser]
Hello everyone, im trying to create a page in wich people could post some comments or questions of a certain record from a data base, (similar to facebook). The issue is that I'm trying to pass the id of a particular database record, but I end up passing the higest (last) id of the loop

Here it is:
model:
Code:
function get_tarea()
    {
        $this->db->select('id_tarea');
        $this->db->select('descripcion');
        $this->db->order_by("id_tarea","ASC");
        $query = $this->db->get('sup_tarea');
        return $query->result();
   }

view

Code:
foreach ($tarea as $row):
  echo form_open('support/question_insert');
  echo form_hidden('id_tarea', $row->id_tarea);// here's where I think the problem is
  echo form_textarea(array('name' => 'question', 'id' => 'question', 'value' => 'enter comment here'));
  echo form_submit( 'submit_comment', 'Enviar');
  form_close();
endforeach;

controller (for the insertion)

Code:
function question_insert(){
  $data_question['question'] = $this->input->post('question');
  $data_question['id_tarea'] = $this->input->post('id_tarea');
  $this->db->insert('sup_question', $data_question);
  redirect('support');
}

The issue is that post('id_tarea') is always the same: the last record from the database.

If anyone wants to see the actual output of this code: http://www.nemoestudio.com/es/support

Thanks in advance for your ideas!
#2

[eluser]bluematt[/eluser]
You need an echo on your form_close(). Your browser is treating this as one big form, and where multiple inputs have the same name, the last one POSTed will be used as the value.
#3

[eluser]Juan Velandia[/eluser]
Hello bluematt... thanks a lot! sometimes my background as a ghraphic designer doesn´t help to solve this kind ok issues. best regards!




Theme © iAndrew 2016 - Forum software by © MyBB