php codeigniter - form helper - How to implement set_value on radio input

hairynuggets picture hairynuggets · Nov 2, 2011 · Viewed 12.5k times · Source

I am in the process of setting up a feedback form for my site using codeigniters form helper.

My form has been created and validation implemented, all works.

My only problem is adding the set_value() function to repopulate the form if an error occurs.

I can't get it to work on my radio inputs, how do you add the set_value() function to a radio type.

Code:

      <ul> 
          <li><?php echo form_radio('found_by', 'newspaper_advert', set_value('found_by')); ?> Newspaper advert</li>
          <li><?php echo form_radio('found_by', 'press_release', set_value('found_by')); ?> Press release</li>
          <li><?php echo form_radio('found_by', 'text_message', set_value('found_by')); ?> Text message</li>
          <li><?php echo form_radio('found_by', 'email', set_value('found_by')); ?> Email</li>
          <li><?php echo form_radio('found_by', 'refferal', set_value('found_by')); ?> Referred to by a friend</li>
          <li><?php echo form_radio('found_by', 'telemarketing', set_value('found_by')); ?> Telemarketing</li>
          <li><?php echo form_radio('found_by', 'leaflet_flyer', set_value('found_by')); ?> Leaflet or flyer</li>
          <li><?php echo form_radio('found_by', 'radio', set_value('found_by')); ?> Radio</li>
          <li><?php echo form_radio('found_by', 'television', set_value('found_by')); ?> Television</li>
          <li><?php echo form_radio('found_by', 'internet_advert', set_value('found_by')); ?> Internet advert</li>
          <li><?php echo form_radio('found_by', 'search_engine', set_value('found_by')); ?> Search engine</li>
          <li><?php echo form_radio('found_by', 'none', set_value('found_by')); ?> None of the above</li>
      </ul>

Answer

swatkins picture swatkins · Nov 2, 2011

set_value only works on inputs and textareas. you need set_radio

set_radio()

Permits you to display radio buttons in the state they were submitted. This function is identical to the set_checkbox() function above.

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

From doc page: http://codeigniter.com/user_guide/helpers/form_helper.html