Welcome Guest, Not a member yet? Register   Sign In
Problems coding for form_radio()
#1

[eluser]Wayne Smallman[/eluser]
Hi guys!

I'm trying to code up radio buttons, but it's not working out.

Code:
$input_common['cash'] = array (
        'name' => 'cash',
        'id' => 'cash',
        'value' => ($this->arrayClassAttributes['results']['entries'][0]['category'] == 'cash') ? $this->arrayClassAttributes['results']['entries'][0]['category'] : set_value('cash'),
        'checked' => true
    );

$input_common['bank'] = array (
        'name' => 'bank',
        'id' => 'bank',
        'value' => ($this->arrayClassAttributes['results']['entries'][0]['category'] == 'bank') ? $this->arrayClassAttributes['results']['entries'][0]['category'] : set_value('bank'),
        'checked' => false
    );

In the form, I have:

Code:
<dt>&lt;?php echo form_label('Cash?', $input_common['cash']['id']); ?&gt;</dt>
<dd>&lt;?php echo form_radio("category", $input_common['cash']); ?&gt;</dd>

<dt>&lt;?php echo form_label('Bank?', $input_common['bank']['id']); ?&gt;</dt>
<dd>&lt;?php echo form_radio("category", $input_common['bank']); ?&gt;</dd>

However, while they're being grouped, it's not checking cash (there isn't even a checked attribute) and their values are being set to Array.

The help documentation isn't all that helpful in terms of syntax. Just saying that form_radio() is almost the same as form_checkbox() isn't as instructive as actually providing code for the grouping, values etc.

Any ideas?
#2

[eluser]mddd[/eluser]
You are mixing up the two uses of form_radio:
1. form_radio($name, $value, $checked_or_not);
2. form_radio($array_of_attributes);

If you provide an array for the attributes of the radio button (method 2), then you should not provide the name separately.
So your form code should be:
Code:
&lt;?php echo form_radio($input_common['cash']); ?&gt;
#3

[eluser]Wayne Smallman[/eluser]
Guys, I've figured it out, and there were two things to consider.

In the first instance, I was doing some checking of values (this page handles both adding and editing), and I had the checking aspect the wrong way around.

Code:
$input_common['cash'] = array (
        'name' => 'cash',
        'id' => 'cash',
        'value' => 'cash',
        'checked' => ($this->arrayClassAttributes['results']['entries'][0]['category'] == 'cash') ? true : false
    );

$input_common['bank'] = array (
        'name' => 'bank',
        'id' => 'bank',
        'value' => 'bank',
        'checked' => ($this->arrayClassAttributes['results']['entries'][0]['category'] == 'bank') ? true : false
    );

In the second instance, form_radio() doesn't appear to like being supplied an array as the second argument. So I supplied actual values for each argument instead.

Code:
<dt>&lt;?php echo form_label('Cash?', $input_common['cash']['id']); ?&gt;</dt>
<dd>&lt;?php echo form_radio("category", $input_common['cash']['value'], $input_common['cash']['checked']); ?&gt;</dd>

<dt>&lt;?php echo form_label('Bank?', $input_common['bank']['id']); ?&gt;</dt>
<dd>&lt;?php echo form_radio("category", $input_common['bank']['value'], $input_common['bank']['checked']); ?&gt;</dd>
#4

[eluser]Wayne Smallman[/eluser]
[quote author="mddd" date="1271779253"]You are mixing up the two uses of form_radio:
1. form_radio($name, $value, $checked_or_not);
2. form_radio($array_of_attributes);

If you provide an array for the attributes of the radio button (method 2), then you should not provide the name separately.
So your form code should be:
Code:
&lt;?php echo form_radio($input_common['cash']); ?&gt;
[/quote]
Hi! I just replied, but didn't see your reply first.

As I said, it's not totally clear in the documentation.

Thanks all the same.
#5

[eluser]Wayne Smallman[/eluser]
Hi guys, I have another problem — how do I get the page to "remember" the values of the radio buttons?

So far, all of the other form elements work fine using set_value(), but I can't figure out how to make form_radio() work the same way as the others.

Is there a way to combine the form_radio() with set_radio() functions? I've been trying, but no joy so far.
#6

[eluser]bhogg[/eluser]
set_radio() isn't really designed for use with form_radio but rather with a manual &lt;input type="radio" ... /&gt; tag. The hack of a workaround is something like (replacing users/dashboard with your form):

Code:
<div>

&lt;?php echo form_open("users/dashboard"); ?&gt;
&lt;?php echo form_radio("testing", "val1", trim(set_radio("testing", "val1")) == 'checked="checked"' ? true : false ); ?&gt; VAL1<br />
&lt;?php echo form_radio("testing", "val2", trim(set_radio("testing", "val2")) == 'checked="checked"' ? true : false ); ?&gt; VAL2<br />

&lt;?php echo form_submit("submit", "submit"); ?&gt;

<pre>
&lt;?php var_dump(set_radio("testing", "val1")); ?&gt;
&lt;?php var_dump(set_radio("testing", "val2")); ?&gt;
</pre>

&lt;?php echo form_close(); ?&gt;

</div>

I just put $this->load->library('form_validation'); in the controller to get this working.
#7

[eluser]Pascal Kriete[/eluser]
You can still use set_value(), you just need to compare it to what you expect to get:
Code:
&lt;?php echo form_radio("testing", "val1", (set_value("testing") == "val1") ); ?&gt; VAL1<br />
&lt;?php echo form_radio("testing", "val2", (set_value("testing") == "val2") ); ?&gt; VAL2<br />




Theme © iAndrew 2016 - Forum software by © MyBB