php Codeigniter中单选按钮的设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22783962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting value of radio button in Codeigniter
提问by Jenz
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 ?
在我用于编辑详细信息的表单中,我有一个带有值0
和1
. 我想要的是,在加载表单时,即当用户单击edit
链接时,应将单选按钮设置为数据库中的值。一旦用户更改它并提交表单,它应该保留提交的值。这怎么可能?
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.
我尝试了下面的代码,但没有用。我知道这样是不行的。$row->videotype
具有来自数据库的值。
<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.
谁能帮我解决这个问题?提前致谢。
回答by Girish
The set_value
function will useful in edit form validation, try this way
该set_value
功能在编辑表单验证中很有用,试试这种方式
<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
编辑:set_radio
编辑模式表单验证中的函数创建问题请使用set_value
函数
回答by Paul Gregory
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:
这set_radio
是一个记录不足的 CI 功能。你没有正确使用它。你最好不要使用它。这更简单:
<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
回答by Yatin Mistry
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'); ?>
回答by Zeeshan
You can use the form_validation library and do this in a much elegant way.
您可以使用 form_validation 库并以非常优雅的方式执行此操作。
You can use the form_radio()
with set_radio()
as follows:
您可以使用form_radio()
withset_radio()
如下:
$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.
这适用于 PHP 5.6 上的 Codeigniter 3,也适用于其他版本。
回答by bharat
Using codeignitor form function:
使用 codeignitor 表单函数:
<?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'); ?>