• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repopulate a Dropdown

#1
[eluser]digitaldivaradio[/eluser]
I have two drop down menus that are a simple arrays, occupations and states. This is a new user reg form. Both drop downs are required when the user submits the form.

When the user submits and fails validation all my text fields are repopulating, but not the drop-downs. I would like to make it easier on the user. Can you help me find the right way to repopulate a drop down?

Here is my code.


VIEW

Code:
$occupation = array(
    '' => 'SELECT YOUR OCCUPATION:',
    '1' => 'Architect / Designer',
    '2' => 'Landscape Architect / Designer',
    '3' => 'Commercial Contractor',
);
<?php        
    echo form_label('Occupation: <span class="requiredinfo">*</span>', 'occupation');
    echo form_dropdown('occupation', $occupation, '');

?&gt;

CONTROLLER

Code:
$this->form_validation->set_rules('occupation', 'Occupation', 'trim|required');

if ($this->form_validation->run() == FALSE)
{
    $this->signup();
}

#2
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...elper.html

Quote:form_dropdown()

Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected.

#3
[eluser]digitaldivaradio[/eluser]
I understand that, I know I can set the select value. I want it to repopulate if the form fails with the user's selection. not mine.

#4
[eluser]Jamie Rumbelow[/eluser]
Use the set_select() function in the form validation library. Read the docs on it - it's a really useful little function.

#5
[eluser]danmontgomery[/eluser]
[quote author="digitaldivaradio" date="1276032699"]I understand that, I know I can set the select value. I want it to repopulate if the form fails with the user's selection. not mine.[/quote]

So you would pass the submitted values to the view, and use the 3rd parameter of form_dropdown()

Code:
echo form_dropdown('occupation',$occupation,$this->input->post('occupation'));

#6
[eluser]Lawrence Leung[/eluser]
[quote author="Jamie Rumbelow" date="1276033695"]Use the set_select() function in the form validation library. Read the docs on it - it's a really useful little function.[/quote]

I am not sure how to use the "set_select" which mentioned in the document. In the documentation, it used the "set_select" to the "options" one by one. But in the form_dropdown, I passed an array to it. Therefore, I don't understand how to apply the "set_select" in it.

I used $this->input->post('xxxxxxx') to re-populate the dropdown form.

e.g.

&lt;?php echo form_dropdown('article_material', $material_options, $this->input->post('article_material'), 'id="article_material"'); ?&gt;

When it is in a update form, which required a default value:

&lt;?php echo form_dropdown('article_material', $material_options, $this->input->post('article_material') ? $this->input->post('article_material') : $article['article_material'], 'id="article_material"'); ?&gt;

#7
[eluser]bsauls[/eluser]
This works for me with 2.02 and automatic form validation via config file.
Code is from the view within form, form fieldset:
Code:
&lt;?php $current_year = date("Y");
$next_year = strtotime ( '+1 year' , strtotime (date("Y") ) );
$next_year = date("Y", $next_year);
$options = array(
  $current_year  => $current_year,
  $next_year    => $next_year,
  );?&gt;
<label for="year" style="width:176px">Application Period beginning in:</label>
<select name="year" id="year" style="width:56px" tabindex="20" >
&lt;?php echo '<option value="'.$current_year.'" '.set_select('year',$current_year, set_value('year'==$current_year)).'>'.$current_year.'</option>'?&gt;
&lt;?php echo '<option value="'.$next_year.'" '.set_select('year',$next_year, set_value('year'==$next_year)).'>'.$next_year.'</option>'?&gt;
If set_value('year'==$next_year) is true, it generates selected="selected" in the HTML for that option. I hope this helps someone.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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