Welcome Guest, Not a member yet? Register   Sign In
Pre-populate select and radio
#1

[eluser]dibs[/eluser]
I am working on a form for updating a users details at the moment and have been pre-populating the form on it's initial load. This works fine with set_value() using the third parameter for the variable I need to prepopulate it with but I also have a select list and a radio selection I need to prepopulate too.

Is it possible to do something like set_value() with these or do I need to write a custom function to achieve this?


Many thanks.
#2

[eluser]pistolPete[/eluser]
Just read the user guide: http://ellislab.com/codeigniter/user-gui...elper.html

Quote:set_select()
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

set_radio()
Permits you to display radio buttons in the state they were submitted.
#3

[eluser]dibs[/eluser]
Pre-populate, not re-populate.

I need to populate with the details pulled from the database on initial load.
#4

[eluser]pistolPete[/eluser]
You should be able to achieve this using the third parameter:

Quote:... the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
#5

[eluser]dibs[/eluser]
Ok so do you mean I need to write something like:

&lt;input type='radio' name="gender" value="female" &lt;?php if($gender=='female' || $gender=='unknown'){ echo "checked";} ?&gt; /&gt;

or can I do something with out the if statement?

I don't see how I can use a var to pre-populate with this structure, or am I totally missing something here. if the third parameter could be a var value instead of the boolean might be good for this.

A)&lt;input type="radio" name="gender" value="female" &lt;?php echo set_radio('gender', 'female', TRUE); ?&gt; /&gt;

B)&lt;input type="radio" name="gender" value="female" &lt;?php echo set_radio('gender', 'female', $gender); ?&gt; /&gt;

Or can I already do something like this?
#6

[eluser]jmadsen[/eluser]
Hi dibs,

I hope there's a cleaner hack, but this is what I've come up with:

Code:
$checked = 0;if($checked){$true=1;$false=0;}else{$true=0;$false=1;}
echo form_radio('tester', 0, $false);
echo form_radio('tester', 1, $true);


$checked = 1;if($checked){$true=1;$false=0;}else{$true=0;$false=1;}
echo form_radio('tester2', 0, $false);
echo form_radio('tester2', 1, $true);
#7

[eluser]Nexus Rex[/eluser]
Here is the cleanest way that I have discovered:
Code:
&lt;?php echo form_radio('active', '1', FALSE, (1 == $member->active) ? set_radio('active', $member->active, TRUE) : set_radio('active', '1')); ?&gt; <label for="active">Active</label>
&lt;?php echo form_radio('active', '0', FALSE, (0 == $member->active) ? set_radio('active', $member->active, TRUE) : set_radio('active', '0')); ?&gt; <label for="active">Inactive</label>

It basically tests the pre-population value (from database) against each radio button value to see if it's a match — if it is then it uses the third parameter of set_radio() to set it as checked — if it is not a match then it uses the standard use of set_radio() for re-populating the submitted form.
#8

[eluser]RobertB.[/eluser]
Code:
<label for="active">Active: </label>&lt;input type="radio" name="active" id="active" value="1" &lt;?=($entry-&gt;active == 1) ? set_radio('active', $entry->active, TRUE) : set_radio('active', '1') ?&gt; />
<label for="active">Inactive: </label>&lt;input type="radio" name="active" id="active" value="0" &lt;?=($entry-&gt;active == 0) ? set_radio('active', $entry->active, TRUE) : set_radio('active', '0') ?&gt; />
#9

[eluser]Wayne Smallman[/eluser]
Hi guys, I've got a similar problem, but it's a little more complex.

When the visitor creates a new item, or edits an existing one, the radio values are set. But if they fail to fill the form out correctly (by making a mistake with a mandatory field), the values of the radio buttons is lost.

This is because I have no variable to check against on error, so the checked value is always unknown if the user doesn't fill out the form correctly.

I have no problems with any of the other examples of set_value() used elsewhere for other fields, it's just set_radio() that won't work because I don't know what variable I'm looking for to check against.

Any ideas?
#10

[eluser]jentree[/eluser]
I realize this is an old post but thought I'd give my solution for select inputs for anyone else who might come across this thread.

This is from an "update" screen, so I wanted to set the selected option based on what was coming back from the db.

Code:
<option value="My Value" &lt;?php echo ($mything[0]->value == 'My Value') ? 'selected="selected"' : '';  ?&gt;> My Value </option>




Theme © iAndrew 2016 - Forum software by © MyBB