PHP - Codeigniter radio buttons

放肆的年华 提交于 2019-12-11 23:45:18

问题


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>

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?


回答1:


You can use the form helper, which has a function called "set_radio", the very last function on this page;

https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

<input type="radio" name="time" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?> />
<input type="radio" name="time" value="parttime" <?php echo set_radio('time', 'parttime'); ?> />

I hope this helps.




回答2:


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'); ?> />

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

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>

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.

ALSO NOTE In your controller, you will need to load the form_validation library for any of this to work.

php

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

Hope this helps!

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?



来源:https://stackoverflow.com/questions/27923940/php-codeigniter-radio-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!