PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

pre checking radio button on form render

  1. Hi there,

    I've written the below based on one of your code solutions on another post and it works brilliantly. However I would like to know how to make the "None" radio button "checked" when the form renders.

    [php]
    add_filter("gform_pre_render_20", "populate_radios");
    
    function populate_radios($form){
    	global $post;
    	$fieldstochange = array(33,35,36,37,38,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88);
    	foreach ($fieldstochange as $fieldtochange) {
        //Creating choices
        $choices = array();
        			if(get_post_meta($post->ID,'ecpt_option1',true)) {
                        $choices[] = array("text" => "Option 1", "value" => "Option 1|10");
                    }
                     if(get_post_meta($post->ID,'ecpt_option2',true)) {
                       $choices[] = array("text" => "Option 2", "value" => "Option 2|10");
                    }
                     if(get_post_meta($post->ID,'ecpt_option3',true)) {
                        $choices[] = array("text" => "Option 3", "value" => "Option 3|10");
                     }
                      if(get_post_meta($post->ID,'ecpt_option4',true)) {
                        $choices[] = array("text" => "Option 4", "value" => "Option 4|10");
                     }
                      if(get_post_meta($post->ID,'ecpt_option5',true)) {
                        $choices[] = array("text" => "Option 5", "value" => "Option 5|10");
                     }
                        $choices[] = array("text" => "None", "value" => "None");
    
        $inputs = array();
        			 if(get_post_meta($post->ID,'ecpt_option1',true)) {
                        $inputs[] = array("label" => "Option 1", "id" => $fieldtochange.'.1');
                     }
                      if(get_post_meta($post->ID,'ecpt_option1',true)) {
                        $inputs[] = array("label" => "Option 2", "id" => $fieldtochange.'.2');
                     }
                      if(get_post_meta($post->ID,'ecpt_option1',true)) {
                        $inputs[] = array("label" => "Option 3", "id" => $fieldtochange.'.3');
                     }
                      if(get_post_meta($post->ID,'ecpt_option1',true)) {
                       $inputs[] = array("label" => "Option 4", "id" => $fieldtochange.'.4');
                     }
                      if(get_post_meta($post->ID,'ecpt_option1',true)) {
                        $inputs[] = array("label" => "Option 5", "id" => $fieldtochange.'.5');
                     }
                        $inputs[] = array("label" => "None", "id" => $fieldtochange.'.6');
    
        //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field){
            //replace 2 with your checkbox field id
            if($field["id"] == $fieldtochange){
                $field["choices"] = $choices;
                $field["inputs"] = $inputs;
            }
        }
    }
        return $form;
    }

    thanks in advance!

    Posted 7 years ago on Thursday December 1, 2011 | Permalink
  2. Hi Tom,

    You should be able to add an "isSelected" property to the choices array, like so:

    [php]
    $choices[] = array("text" => "None", "value" => "None", "isSelected" => true);

    Let me know if that does the trick.

    Posted 7 years ago on Thursday December 1, 2011 | Permalink
  3. Awesome, works a treat, thanks David!

    Posted 7 years ago on Friday December 2, 2011 | Permalink
  4. Glad to help. :)

    Posted 7 years ago on Friday December 2, 2011 | Permalink

This topic has been resolved and has been closed to new replies.