[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><?php echo form_label('Cash?', $input_common['cash']['id']); ?></dt>
<dd><?php echo form_radio("category", $input_common['cash']); ?></dd>
<dt><?php echo form_label('Bank?', $input_common['bank']['id']); ?></dt>
<dd><?php echo form_radio("category", $input_common['bank']); ?></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?
[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: <?php echo form_radio($input_common['cash']); ?>
[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><?php echo form_label('Cash?', $input_common['cash']['id']); ?></dt>
<dd><?php echo form_radio("category", $input_common['cash']['value'], $input_common['cash']['checked']); ?></dd>
<dt><?php echo form_label('Bank?', $input_common['bank']['id']); ?></dt>
<dd><?php echo form_radio("category", $input_common['bank']['value'], $input_common['bank']['checked']); ?></dd>
[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: <?php echo form_radio($input_common['cash']); ?>
[/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.
[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.
[eluser]bhogg[/eluser]
set_radio() isn't really designed for use with form_radio but rather with a manual <input type="radio" ... /> tag. The hack of a workaround is something like (replacing users/dashboard with your form):
Code: <div>
<?php echo form_open("users/dashboard"); ?>
<?php echo form_radio("testing", "val1", trim(set_radio("testing", "val1")) == 'checked="checked"' ? true : false ); ?> VAL1<br />
<?php echo form_radio("testing", "val2", trim(set_radio("testing", "val2")) == 'checked="checked"' ? true : false ); ?> VAL2<br />
<?php echo form_submit("submit", "submit"); ?>
<pre>
<?php var_dump(set_radio("testing", "val1")); ?>
<?php var_dump(set_radio("testing", "val2")); ?>
</pre>
<?php echo form_close(); ?>
</div>
I just put $this->load->library('form_validation'); in the controller to get this working.
[eluser]Pascal Kriete[/eluser]
You can still use set_value(), you just need to compare it to what you expect to get:
Code: <?php echo form_radio("testing", "val1", (set_value("testing") == "val1") ); ?> VAL1<br />
<?php echo form_radio("testing", "val2", (set_value("testing") == "val2") ); ?> VAL2<br />
|