CodeIgniter form_radio with set_value
Asked Answered
I

5

7

I am having a bit of trouble with my form and radio buttons, with input text I do this:

<?php echo form_input('lastname', set_value('lastname'), 'id=lastname'); ?>
<?php echo form_error('lastname'); ?>

and when the validation runs and that input field that is filled out gets the valued returned...what I am looking for is a way to do this with radio buttons

<tr><td><?php echo form_label('Gender: ', 'gender'); ?></td><td><?php echo form_label('Male', 'male') . form_radio('gender', 'M', '', 'id=male'); ?><br><?php echo form_label('Female', 'female') . form_radio('gender', 'F', '', 'id=female'); ?></td><td><?php echo form_error('gender'); ?></td></tr>

as you can see both my radio buttons have values already F or M.....how do I have the button that is selected returned selected?

Indihar answered 7/11, 2012 at 0:24 Comment(0)
C
2

This worked well for me. Even though I didnt use form_helper, this was much easier way. Everyone face issue to take input for gender field :)

               <input id="gender" name="gender" type="radio" class="" <?php if($gender=='0') echo "checked='checked'"; ?> value="0" <?php echo $this->form_validation->set_radio('gender', 0); ?> />
                <label for="gender" class="">Male</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='1') echo "checked='checked'"; ?> value="1" <?php echo $this->form_validation->set_radio('gender', 1); ?> />
                <label for="gender" class="">Female</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='2') echo "checked='checked'"; ?> value="2" <?php echo $this->form_validation->set_radio('gender', 2); ?> />
                <label for="gender" class="">Others</label>

I hope it will help someone :)

Cyclone answered 24/9, 2014 at 9:30 Comment(2)
Just a minor comment, the {} braces are missing from the if clauseBengurion
@RomanT, considering that there is only statement with "if" condition, braces are not mandatory, but good to have.Cyclone
O
10

From the user guide: https://www.codeigniter.com/user_guide/helpers/form_helper.html

form_radio()

This function is identical in all respects to the form_checkbox() function above except that is sets it as a "radio" type.

So reading further:

form_checkbox()

Lets you generate a checkbox field. Simple example:

echo form_checkbox('newsletter', 'accept', TRUE);

Would produce:

<input type="checkbox" name="newsletter" value="accept" checked="checked" />

The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.

So in your case, it might be something like this:

// Pass boolean value to third param
// Example:
$radio_is_checked = $this->input->post('gender') === 'F';

echo form_radio('gender', 'F', $radio_is_checked, 'id=female');

Since set_radio() just returns a string checked="checked", you could wedge it in to the fourth paramter if you really wanted to but it makes for some ugly looking code:

echo form_radio('gender', 'F', NULL, 'id="female" '.set_radio('gender', 'F'));
Oxonian answered 7/11, 2012 at 0:33 Comment(0)
B
6

I use Ternary Operator for this. Suppose you got return value of "M" (Male). I am making $selected variable for example and assume that it has value of M.

<tr>
    <td>
        <?php echo form_label('Gender: ', 'gender'); ?>
    </td>
    <td>
        <?php echo form_label('Male', 'male') . form_radio(array('name' => 'gender', 'value' => 'M', 'checked' => ('M' == $selected) ? TRUE : FALSE, 'id' => 'male')); ?><br>
        <?php echo form_label('Female', 'female') . form_radio(array('name' => 'gender', 'value' => 'F', 'checked' => ('F' == $selected) ? TRUE : FALSE, 'id' => 'female')); ?>
    </td>
    <td>
        <?php echo form_error('gender'); ?>
    </td>
</tr>
Breastpin answered 9/11, 2015 at 6:11 Comment(0)
C
2

This worked well for me. Even though I didnt use form_helper, this was much easier way. Everyone face issue to take input for gender field :)

               <input id="gender" name="gender" type="radio" class="" <?php if($gender=='0') echo "checked='checked'"; ?> value="0" <?php echo $this->form_validation->set_radio('gender', 0); ?> />
                <label for="gender" class="">Male</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='1') echo "checked='checked'"; ?> value="1" <?php echo $this->form_validation->set_radio('gender', 1); ?> />
                <label for="gender" class="">Female</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='2') echo "checked='checked'"; ?> value="2" <?php echo $this->form_validation->set_radio('gender', 2); ?> />
                <label for="gender" class="">Others</label>

I hope it will help someone :)

Cyclone answered 24/9, 2014 at 9:30 Comment(2)
Just a minor comment, the {} braces are missing from the if clauseBengurion
@RomanT, considering that there is only statement with "if" condition, braces are not mandatory, but good to have.Cyclone
I
1

This works forsure....

<tr><td><?php echo form_label('Gender: ', 'gender'); ?></td><td><?php echo form_label('Male', 'male') . form_radio(array("name"=>"gender","id"=>"male","value"=>"M", 'checked'=>set_radio('gender', 'M', FALSE))); ?><br><?php echo form_label('Female', 'female') . form_radio(array("name"=>"gender","id"=>"female","value"=>"F", 'checked'=>set_radio('gender', 'F', FALSE))); ?></td><td><?php echo form_error('gender'); ?></td></tr>

wrote my radio buttons like so

<?php form_radio(array("name"=>"gender","id"=>"female","value"=>"F", 'checked'=>set_radio('gender', 'F', FALSE))); ?>
Indihar answered 7/11, 2012 at 0:35 Comment(2)
checked accepts a Boolean, while set_radio returns checked="checked".Meniscus
The reason this works is because CI is nice enough to correct your error and convert the value of checked to a legal one, with any other attribute this would output something like attr=" checked="checked"".Oxonian
C
-2
$_fldProyectoMostrar = array(
"id"        => "_FLDPROYDER",
"name"      => "_FLDPROYDER"
);


<?php echo form_radio($_fldProyectoMostrar, "FALSE",    set_radio("_FLDPROYDER", "FALSE", TRUE)); ?>NO

<?php echo form_radio($_fldProyectoMostrar, "TRUE",     set_radio("_FLDPROYDER", "TRUE", FALSE )); ?>SI

Donde el el segundo valor del set radio, debe ser = al string value del form_radio

Crispen answered 20/4, 2014 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.