Reputation: 8369
In my form for editing details, I have a radio button with values 0
and 1
. What I want is, while the form is loaded ie, when user clicks on edit
link, the radio button should be set to the value in the database. Once the user have changed it and submit the form, it should retain the submitted value. How this is possible ?
I tried with the below code and it doesn't worked. I know that it can't be done this way. $row->videotype
have the value from database.
<input type="radio" name="trailer" value="1" <?php
if(set_radio('trailer', '1', TRUE)) {
echo set_radio('trailer', '1', TRUE);
} else {
if($row->videotype==1) ?> checked=checked <?php
} ?> />Yes
<input type="radio" name="trailer" value="0" <?php
if(set_radio('trailer', '0')) {
echo set_radio('trailer', '0');
} else {
if($row->videotype==0) ?> checked=checked <?php
} ?> />No
In form submission function (in controller) , I have set this for form validation :
$this->form_validation->set_rules('trailer', 'Trailer', 'is_numeric');
Can anyone help me to fix this? Thanks in advance.
Upvotes: 1
Views: 35343
Reputation: 1776
Using codeignitor form function:
<?php $selected = 'Yes'; ?>
<?php echo form_label('Trailer: ', 'trailer'); ?>
<?php echo form_label('Yes', 'trailer_yes') . form_radio(array('name' => 'trailer', 'value' => 'Yes', 'checked' => ('Yes' == $selected), 'id' => 'trailer_yes')); ?>
<?php echo form_label('No', 'trailer_no') . form_radio(array('name' => 'trailer', 'value' => 'No', 'checked' => ('No' == $selected), 'id' => 'trailer_no')); ?>
<?php echo form_error('trailer'); ?>
Upvotes: 1
Reputation: 515
You can use the form_validation library and do this in a much elegant way.
You can use the form_radio()
with set_radio()
as follows:
$this->load->library('form_validation');
echo form_radio('trailer', '1', set_radio('trailer', '1', $row->videotype === "1" ? TRUE : FALSE));
echo form_radio('trailer', '0', set_radio('trailer', '0', $row->videotype === "0" ? TRUE : FALSE));
This works for me on Codeigniter 3 on PHP 5.6, should work on other versions too.
Upvotes: 0
Reputation: 100
Check below for default value check
<?php
echo $gender = $this->input->post('gender');
if($gender=='male' || empty($gender)) $mchecked=true;
else if($gender=='female') $fchecked=true;
?>
<?php echo form_label('Gender: ', 'gender'); ?><br />
<?php echo form_radio('gender', 'male', @$mchecked, 'id=male').form_label('Male', 'male'); ?>
<?php echo form_radio('gender', 'female', @$fchecked, 'id=female').form_label('Female','female'); ?>
Upvotes: 0
Reputation: 12117
The set_value
function will useful in edit form validation, try this way
<input type="radio" name="trailer" value="1" <?php
echo set_value('trailer', $row->videotype) == 1 ? "checked" : "";
?> />Yes
<input type="radio" name="trailer" value="0" <?php
echo set_value('trailer', $row->videotype) == 0 ? "checked" : "";
?> />No
In the controller the validation rules fine, please also use trim rule in case post data have blank space
$this->form_validation->set_rules('trailer', 'Trailer', 'trim|is_numeric');
EDIT: the set_radio
function create issue in edit mode form validation please use set_value
function
Upvotes: 8
Reputation: 1753
The set_radio
is a poorly documented CI function. You're not using it right. You're better off not using it. This is simpler:
<input type="radio" name="trailer" value="1" <?php if($row->videotype==1) ?> checked=checked <?php } ?> />Yes
<input type="radio" name="trailer" value="0" <?php if($row->videotype==0) ?> checked=checked <?php } ?> />No
Upvotes: 1