How to change which choice in a Gravity Forms radio button field is selected using PHP as part of a gform_pre_submission [RESOLVED]

I can not find a code example that works.

I want to set a Product Option Radio Button Field’s selected value during a gform_pre_submission add_action based on another field’s value. I’ve tried something like this (there are other functions and variables are defined elsewhere in advance, this is an abstract) but this approach isn’t working for me and I am wondering what I am missing:

add_action( 'gform_pre_submission_FORMIDhere', 'select_radio_button' );

function select_radio_button( $form) {

  foreach( $form['fields'] as &$field ) {
    
    if( 53 === $field->id ) {
     
      foreach( $field->choices as &$choice ) {  
      echo($aag_kg_nummorn.' / /'.$choice['value']);
        if( $aag_kg_nummorn == $choice['value'] ) {
            echo('Bingo!');
          $choice['isSelected'] = true;  // <- THIS line doesn't work for me
        }
      } 
    } 
  } 
  return $form;
}

Thank you in advance!

Hello! If you are trying to set the value of a radio button, when the submit button is pressed, before the entry is stored, I think you can use the gform_pre_submission_FORMID filter, but you will need to set the value in the $_POST. Setting values in the $form is only useful on pre_render.

Example 2 here shows reading one field’s value, and assigning that value to another field:

You’re not doing exactly that, but this does show how to read the value (rgpost with the input ID) and then how to set that radio button value (you do that directly in the $_POST).

Does that help?

Thank you, I was using $_POST for other values, but I think I may still have been having trouble targeting and setting a specific choice as selected. Would you be able to share what that single line of code might look like in a generic one line example?

My programming skills are a little like I am standing at the edge of a cliff. With a little push from someone more knowledgeable, I am easily able to make it to the bottom surprisingly quickly. :sunglasses:

Thank you for your help on this, it is greatly appreciated.

I see now that I missed where the cliff edge was. Setting the VALUE of the Radio Button Field by $_POST is how to change it’s selected state for my purpose… doh! Thank you to Richard at Gravity Forms for pointing me in the right directioooooooooooonnnnnnnnn!

Oops, no, that’s not the only factor (and I did try that before), it’s that it doesn’t appear to work for me for a Product Option Radio Button field. That’s where I am having difficulty changing the choice using the $_POST method. So I am still searching for an answer.

Do you already have a ticket open with Richard on this one? If so, I think I would keep working there rather than in the forums, as this will take longer.

Thank you Chris, Richard has indeed pointed me further in the right direction. I wasn’t understanding the format of the array value. It is working now with this:

Substituting

$_POST['input_53'] = $choice['value'] . '|'. GFCommon::to_number( $choice['price'] );

for this line:

$choice['isSelected'] = true; // <- THIS line doesn't work for me

Has it working for me now. It was the “divider” symbol in the @_POST format that I was not taking into account.

Leaving this here for someone else looking over the cliff in the future.

1 Like