• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set_value, set_select for form_multiselect in v2

[eluser]Brad K Morse[/eluser]
Using version 2

Unable to retain the items selected by the user within a form_multiselect when the form is redisplayed after the user did not complete the entire form

// controller
$this->form_validation->set_rules('goal_id[]', 'Goal', 'required'); // rule
$form_view['goals'] = $this->add_model->getGoals(); // getting goals
$this->load->view('add-assessment/form-view', $form_view);

// model
function getGoals() {
$q = $this->db->order_by('id', 'asc')->get('goal');
if($q->num_rows() > 0) {
   foreach($q->result() as $row) {
    $data[$row->id] = $row->title;
   return $data;

// view
<?=form_multiselect('goal_id[]', $goals, set_select('goal_id[]'))?>

It does not retain any items selected by the user when using set_select, but it retains one item if you use set_value

According to the user guide, set_select() is intended to be used when you're generating the select HTML by hand, like this:

<select name="myselect">
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
<option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
<option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>

Instead, form_multiselect() wants an array of values as its third parameter. The $_POST array will contain whatever values the user selected:

&lt;?=form_multiselect('goal_id[]', $goals, $_POST['goal_id[]'])?&gt;

Or if you're using CI's Input class, then something like this:

&lt;?=form_multiselect('goal_id[]', $goals, $this->input->post('goal_id[]'))?&gt;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.