CodeIgniter Forums
Can't get set_select() to work - 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: Can't get set_select() to work (/showthread.php?tid=18898)



Can't get set_select() to work - El Forum - 05-21-2009

[eluser]EddieX[/eluser]
Hi!

First of all I must say that I really enjoy this framework, it's really neat!

Though, I have a problem when it comes to repopulate form's and especially when I'm using select-tag and checkboxes.

I use the following in my view, which I think should do the trick but it does not...
Code:
$roles = array();
foreach($available_roles as $role)
{
    $roles[$role['id']] = $role['name'];
}

$role_dropdown = form_dropdown('role', $roles, set_value('role'));
?>

$available_roles is an array of the following format: arr[x][(id, name)], and it is set by the controller.

Any suggestions?


Can't get set_select() to work - El Forum - 05-21-2009

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

EDIT: Deleted, because I totally missed the point.


Can't get set_select() to work - El Forum - 05-21-2009

[eluser]Jondolar[/eluser]
I would check the value of set_value('role') to see what it is set to when you submit your form. Also, check to make sure you don't have any other form element called 'role' that may be overwriting that value.


Can't get set_select() to work - El Forum - 05-21-2009

[eluser]EddieX[/eluser]
I must say that I'm really impressed by the response times, thanks!

I have now printed the form-value for role (controller side) and it is the chosen id (role['id']), which is unsigned integers. There is only one form in the view so there should not be a problem regardin duplicate names etc.

I guess it is possible to solve the problem by parsing the $_POST @ controller and then feed the proper data to the view and then let the view generate a select-tag from that (manually), but it does not feel like the CI-way..

All suggestions are welcome!


Can't get set_select() to work - El Forum - 05-21-2009

[eluser]Jondolar[/eluser]
If you hard-code an index into form_dropdown() does it work? Does it select the proper item (albiet every time)? Maybe there is a bug in that function?


Can't get set_select() to work - El Forum - 05-22-2009

[eluser]EddieX[/eluser]
Today I have done some more debugging and it seems that the "set_value()" in the view returns an empty string. It works just fine for textbox/textareas.

I tried the following:

In the view it looks like this (where the options are fetched from db, but that should not matter).
Code:
<select name="role">
   <option value="1" >Administrator</option>
   <option value="2" >User</option>
</select>

When the view is loaded I expicitly print the value:
Code:
echo("DEBUG:".set_value('role'));
I also print the value from the POST-variable which actually has a value when the form is submitted but the set_value('role') always returns an empty string.

The actual output from the view is:
Code:
role=1 DEBUG:
where role=X is put there by the controller.

Does this mean that there are a problem with select-tag and set_value(), or what do you think?


Can't get set_select() to work - El Forum - 05-22-2009

[eluser]EddieX[/eluser]
An easy solution:

In controller:
Code:
...
$data['form_role'] = $_POST['role']; //If it exists
$this->load->view('admin/account_create', $data);

In view (account_create):
Code:
$roles = array();
foreach($available_roles as $role)
{
    $roles[$role['id']] = $role['name'];
}

$role_dropdown = form_dropdown('role', $roles, $form_role);

This solve the problem, but it should not be necessary to do it this way though.