Reputation: 33
Why i cant get my array data from views into my controller?
Views:
<?php
foreach($get_q->result() as $gq){
?>
<input class="form-control" value="<?php echo $gq->id; ?>" name="hidden" type="hidden">
<?php
}
?>
Controller
if($this->form_validation->run()==FALSE)
{
//true
$this->load->model("mymodel");
for($i=0; $i<count($this->input->post('hidden')); $i++){
$data = array(
'question_id' => $this->input->post("hidden")[$i]
);
if($this->input->post("insert"))
{
$this->mymodel->my_function($data);
}
}
}
Its working but its only get the last element of the array the other element becomes zero(0).
Like this
Upvotes: 0
Views: 31
Reputation: 54831
name="hidden"
is incorrect. Multiple name="hidden"
means that the last field overwrites all previous values. Use []
notation:
<input class="form-control" value="<?php echo $gq->id; ?>" name="hidden[]" type="hidden">
----- NOTE ^
With this, $this->input->post('hidden')
will be array, as you expect.
Upvotes: 2