Support

Account

Home Forums Front-end Issues Ability for a default checked radio button in a form

Solved

Ability for a default checked radio button in a form

  • Hey, I have a custom form with a list of radio buttons. I’m pulling the values from a checkbox field. I’m trying to find a way to choose the default checked radio button without changing it in the code each time. Maybe I’m not pulling the values the right way? It’s in a foreach loop. I have set a counter to be able to change it, but again, trying to be able to do it without coding. here’s the form snipped:

    
    <div class="single-filter-type">
        <?php  $field = get_field_object('weather'); ?>
        <h3><?php echo $field['label'] ?></h3>
    <?php 
    $checked = ' checked="checked"';
        $weather_values = get_field('weather');                   
        if( $weather_values ):  
    
            $i = 0;//set counter
                foreach($weather_values as $bar) {
                     if($i == 2) { $checked = "checked";}else {$checked = '';}
                     echo "<input type='radio' name='weather' value='{$bar}' {$checked}>";
                     $i++;
    
                } 
       endif; 
    ?> 
    </div> 
    
  • I don’t quite get it. So, get_field('weather') is a checkbox field type, right? But what do you mean with “default checked radio button”? It looks like you are creating radio buttons on the fly from the checked checkboxes, i. e. if three checkboxes are checked, three radio buttons will be created.

    What’s the point of all this?

  • Sorry, will clarify. ‘weather’ is a checkbox field, yes. I have a custom form on the fly that I’m using for ajax filtering with the POST method. I have multiple lists. same as ‘weather’, that I’m using as radio buttons in the form, as I want only one value to be chosen in each list.
    Of course, ‘weather’ doesn’t have to be a checkbox field, I’m just trying to find a way it would be easy to a non coder to add the values. so my issue with the way it works now is the option to choose the default value from the dashboard..

    Thanks for your reply

  • The description of what you are trying to do is still not clear to me.

    Are you trying to show the choices for the checkbox field as a radio input and set the value chosen in the checkbox as the default value for the radio?

  • Ok, I’m trying to have a list of radio inputs in a form on the front, when pulling the values from an acf field (that’s my checkbox field, but it doesn’t have to be a checkbox), and in addition to have the ability to set one of the choices as default checked.

    I tried to achieve it with a checkbox field, and tried with a radio field as well. In both options I’m facing an issue with setting one of the choices as default checked (I don’t have problems with pulling the values).

    Thanks, hopefully it makes sense

  • I solved it. Thanks for your replays. I used a radio button field eventually (instead of the checkbox). here is the code:

    
        <div class="single-filter-type">
            <?php  $field = get_field_object('weather'); ?>
            <h3><?php echo $field['label'] ?></h3>
        <?php 
            $checked = ' checked="checked"';
            $weather_values = get_field('weather');                   
            if( $weather_values ):  
                    foreach( $field['choices'] as $k => $v ){ 
                        if(get_field('weather') == $v) { $checked = "checked";}else {$checked = '';} ?>     <label class="filter-checkbox-item" for="<?php  echo $v;  ?>">
                                <input type="radio" name="weather" value="<?php echo $v;  ?>" <?php echo $checked ?> >
                                <?php echo $v;  ?>
                            </label>
                <?php }   
    
           endif; 
       ?> 
        </div>
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Ability for a default checked radio button in a form’ is closed to new replies.