CodeIgniter Forums
form_dropdown and set_select - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form_dropdown and set_select (/showthread.php?tid=24606)



form_dropdown and set_select - El Forum - 11-14-2009

[eluser]bhbutter123[/eluser]
I am not getting the result I want out of set_select when using form_dropdown. I know how to use both but the set_select is not working. The second setting in the set_select function is for the value of the option but if you are supplying the form_dropdown an array to build a list of options I cannot think of how to set the second setting of the set_select function. Would someone please show me working code so that I can see how it is done.


form_dropdown and set_select - El Forum - 11-14-2009

[eluser]bigtony[/eluser]
The set_select() function is intended for use when you code the select manually. If you're using the form_dropdown() then the third parameter is simply the value to use as the default, so no need to use set_select - just pass the value.


form_dropdown and set_select - El Forum - 11-15-2009

[eluser]Tandubhai[/eluser]
Simply set your value to thrid parameter using form_dropdown() will fulfil your need.
But with check-box and radio-button Codigniter has no any such parameter to set as checked. You can simply create your own helper file to overcome this.
Tandubhai


form_dropdown and set_select - El Forum - 11-18-2009

[eluser]ericrjones1[/eluser]
For the form_dropdown function you don't need to use the set_select function.

For the form_dropdown function use the following code as guidance.

Code:
echo form_dropdown('post_status', $status, set_value('post_status', 'published'), $js);
For this code, $status is an array passed from the controller. The third parameter should be the set_value function. The first parameter in the set_value function will pull the value of the dropdown from $this->input->post('post_status'), and the second is the default value.

For the form_multiselect function use the following code as guidance.

Code:
echo form_multiselect('post_tags[]', $tags, set_value('post_tags[]'), $input);
Same explanation as above, but with form_multiselect.


form_dropdown and set_select - El Forum - 12-22-2009

[eluser]Peter Lachky[/eluser]
Hi slingshoteric,

are you sure your proposal works? Because for me it does not but there is still a chance that I am doing something wrong.

Code:
<?php echo form_dropdown('kategoria', $kategorie, set_value('kategoria', $kategoria)) ?>

does not re-populate. It looks like the only way how to achieve this is:
Code:
<?php echo form_dropdown('kategoria', $kategorie, $this->input->post('kategoria') ? $this->input->post('kategoria') : $kategoria) ?>

Am I right or just missing anything?


form_dropdown and set_select - El Forum - 12-31-2009

[eluser]ericrjones1[/eluser]
Peter,

I am pretty sure that my proposal works.

[quote author="Peter Lachky" date="1261542309"]

Code:
<?php echo form_dropdown('kategoria', $kategorie, set_value('kategoria', $kategoria)) ?>

[/quote]

Your proposed solution for resetting the form...

[quote author="Peter Lachky" date="1261542309"]

Code:
<?php echo form_dropdown('kategoria', $kategorie, $this->input->post('kategoria') ? $this->input->post('kategoria') : $kategoria) ?>

[/quote]

...is similar to what the set_value function is doing (check it out in the CI_Form_Validation Class).

Code:
function set_value($field = '', $default = '')
{
    if ( ! isset($this->_field_data[$field]))
    {
        return $default;
    }
        
    return $this->_field_data[$field]['postdata'];
}

I would check for the following items:

1. $kategoria is a key in the array $kategorie
2. $kategoria is of the expected type and value
3. check the return value of set_value() and how that is different from the $this->input->post() value