Storing multiple inputs with the same name in a CodeIgniter session

codeigniter, foreach, php

Solution

You'd want to use this in your form:

<input type="text" name="goal[]">

You can then get the values in the Controller via:

$goal = $this->input->post('goal');

And then set the variable in the session via:

$this->session->set_userdata('goal', $goal);

If you want to retrieve it again. do this via:

$goal = $this->session->userdata('goal');

You'll have something like this:

$goal[0] = 'first goal';
$goal[1] = 'second goal';

Please try it first :)

Problem

I've posted this in the CodeIgniter forum and exhausted the forum search engine as well, so apologies if cross-posting is frowned upon. Essentially, I've got a single input, set up as `<input type="text" name="goal">`. At a user's request, they may add another goal, which throws a duplicate to the DOM. What I need to do is grab these values in my CodeIgniter controller and store them in a session variable. My controller is currently constructed thusly: ``` function goalsAdd(){ $meeting_title = $this->input->post('topic'); $meeting_hours = $this->input->post('hours'); $meeting_minutes = $this->input->post('minutes'); $meeting_goals = $this->input->post('goal'); $meeting_time = $meeting_hours . ":" . $meeting_minutes; $sessionData = array( 'totaltime' => $meeting_time, 'title' => $meeting_title, 'goals' => $meeting_goals ); $this->session->set_userdata($sessionData); $this->load->view('test', $sessionData); } ``` Currently, obviously, my controller gets the value of each input, writing over previous values in its wake, leaving only a string of the final value. I need to store these, however, so I can print them on a subsequent page. What I imagine I'd love to do is extend the input class to be able to call $this->input->posts('goal'). Eventually I will need to store other arrays to session values. But I'm totally open to implementation suggestion. Thanks so much for any help you can give.

Original source