简体   繁体   中英

PHP - Codeigniter radio buttons

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?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM