PHP - codeigniter单选按钮 [英] PHP - Codeigniter radio buttons

查看:152
本文介绍了PHP - codeigniter单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用 codeigniter - 我显示,像这样的单选按钮:

Im using Codeigniter - I am displaying radio buttons like so :

<fieldset>

<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime"> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime"> Part Time

</fieldset>

这工作得很好,它们显示。
下面是该模型中的code。

Which works fine, they display. Here is the code in the model.

'time'=>$this->input->post('time'),

这也能正常工作,这样可以节省的选择到数据库。但是,我怎么得到的单选按钮来填充在页面加载时从数据库中选择?

which also works fine , it saves the choice to the database. But how do I get the radio button to populate when the page loads with the choice from the database?

推荐答案

正如@Craig表示,采用 set_radio 是你所需要的。

Just as @Craig said, using set_radio is what you will need.

PHP

<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />

注意
此单选按钮的值是2。任何值,需要在set_radio第二个参数

Note The value of this radio button is "2". Whatever the value is, needs to be the second parameter in set_radio.

您code会是这个样子:

Your code would look something like this:

HTML

<fieldset>
    <legend>Part Time / Full Time:</legend><br>
    <input type="radio" name="time" id="radfull" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?>> Full Time<br/>
    <input type="radio" name="time" id="radpart" value="parttime" <?php echo set_radio('time', 'parttime'); ?>> Part Time
</fieldset>

如果您希望这两个选项默认选中(页面加载时),这是您将第三个参数 TRUE 添加到相应的电台。

If you want either option checked by default (when the page loads) that is where you would add the third parameter TRUE to the corresponding radio.

另外请注意
在你的控制器,你将需要加载 form_validation 库任何这个工作。

PHP

$this->load->library('form_validation');

希望这有助于!

修改
我的道歉,我误解了问题。我上面已经是...之前的形式正式/成功提交。你的情况,形式已是(成功)提交,并要...编辑表单(由于缺乏更好的话) - 所以你需要填充与任何用户选择输入的一种方式。

EDIT My apologies, I misread the question. What I had above is...before the form is officially/successfully submitted. In your case, the form has already been (successfully) submitted and you want to...edit the form (for lack of better words) - so you need a way of populating the inputs with whatever the user chose.

我在过去所做的那样 - 在我看来 - 只是有权在元素上的三元操作

What I have done in the past - in my view - is just have a ternary operation right on the element.

例如,假设我返回用户,并有一个时间属性。

For example, let's say I am returning a user, and there is a "time" property.

HTML

<fieldset>
    <legend>Part Time / Full Time:</legend><br>
    <input type="radio" name="time" value="fulltime" <? echo($user['time'] === 'fulltime') ? selected="selected" : '';  ?> />
    <input type="radio" name="time" value="parttime" <? echo($user['time'] === 'parttime') ? selected="selected" : ''; ?> />
</fieldset>

也许这是更接近你在找什么?

Maybe that's closer to what you are looking for?

这篇关于PHP - codeigniter单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆