Quigley
Quigley

Reputation: 13

Remembering CodeIgniter form_dropdown fields

This works...

form_dropdown('location', $location_options, $this->input->post('location'));

But when I try and use an array to add extra attributes, it stops working... Why is this?

$attributes = array(
    'name' => 'location',
    'id' => 'location'
);

form_dropdown($attributes, $location_options, $this->input->post('location'));

The name of the dropdown list is included in the array of attributes so i don't see how this is any different to the first example. Whenever the form is posted posted back, it resets to the start.

Can anyone help me out with this? Thanks

Upvotes: 1

Views: 2568

Answers (2)

Chirag Rafaliya
Chirag Rafaliya

Reputation: 209

    $attributes = ' id="bar" class="foo" onChange="some_function();"';
    $location_options = array(
       'IN' =>'India',
       'US' =>'America'
    );

 form_dropdown('location', $location_options, $this->input->post('location'),$attributes);

Parameters :

  1. 1st param will assign to name of the field,
  2. 2nd will get your options,
  3. 3rd is for default value,
  4. 4th one is for extra properties to add like javascript function, id, class ...

Upvotes: 0

mseo
mseo

Reputation: 3911

It's just the wrong syntax.

Please have a look at the docu: http://codeigniter.com/user_guide/helpers/form_helper.html

form_dropdown('location', $location_options, $this->input->post('location'), "id='location'");

Your code should look something like the above. And by the way: if you're using the form_validation library you could use set_value instead of $this->input->post ...

Upvotes: 1

Related Questions