4

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>

5 Answers 5

9

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

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work for me, im using values 0 and 1 instead of 1 and 2, any clue?
@Chumillas, are you passing 0 and 1 as strings in the set_radio function?
Yes, as same as you do
The problem is when the user didn't upload an image in the form, the validation breaks with 'You did not select a file to upload.' and the set_radio doesn't works anymore. If the user upload an image the set_radio works when another field breaks the validation.
0
<?= form_radio('found_by', 'newspaper_advert', $found_by == 'newspaper_advert'); ?>

Comments

0

Try this, It works for me:

Gender:

< ?php echo form_radio("gender", "Female", NULL, set_radio('gender', 'Female')); ?>Female

< ?php echo form_radio("gender", "Male", NULL, set_radio('gender', 'Male')); ?>Male

Comments

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

it is so easy....

Comments

-1
 <?php echo form_radio( array( 'name'=> 'age_group','id'=>'age_group','class'=> 'input-xsmall focused inputError','type'=>'radio',set_radio('age_group', 'under 18', TRUE) ) ). "under 18"; ?><br />
            <?php echo form_radio( array( 'name'=> 'age_group','id'=>'age_group','class'=> 'input-xsmall focused inputError','type'=>'radio',set_radio('age_group', '18 to 29', TRUE) ) ). "18 to 29"; ?><br />
            <?php echo form_radio( array( 'name'=> 'age_group','id'=>'age_group','class'=> 'input-xsmall focused inputError','type'=>'radio',set_radio('age_group', '30 to 49', TRUE) ) ). "30 to 49"; ?><br />
            <?php echo form_radio( array( 'name'=> 'age_group','id'=>'age_group','class'=> 'input-xsmall focused inputError','type'=>'radio',set_radio('age_group', 'above 49', TRUE) ) ). "above 49"; ?>

It is right code in view ??

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.