views:

113

answers:

1

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.

+4  A: 

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 :)

Thorpe Obazee
Thorpe, thanks so much for taking the time to answer. This is essentially what I have now, but it seems each successive goal rewrites the previous goal, and I end up only with the final value. Does this make sense? Do the brackets at the end of the name value somehow combat this in a way I'm unaware of?
Joshua Cody
It turns it into an array... I will update my answer.
Thorpe Obazee
How about that. I'm not sure why the internet never told me this before. Thanks so much, Thorpe. I'll try this tomorrow and give you the appropriate comment or checkmark.
Joshua Cody
Great, Thorpe! This is now working, thanks so much!
Joshua Cody
no problem. glad I could help.
Thorpe Obazee
Loving "I'm not sure why the internet never told me this before."!I agree it's a little advertised and documented feature; but I think that's because it's a PHP specific one; the HTML spec lets you name an input what you want, it's PHP which is seeing the square brackets and creating an array.
Kurucu