Submit dynamically populated field only if radio button selected

I have PHP code in my functions.php that populates a hidden form field with what is a gift code. The field is populated immediately.
The user receives a gift if they donate over $20.
I have radio buttons
Donate under $20
Donate $20 to $34
Donate $35 or more
How can I have the gift code field NOT submit to the database when under $20 is selected?
How can I populate the gift field only after a radio button is selected. Like “if radio button NOT under $20” populate gift code field on submit.
Here is my code to populate the gift field with a sequential gift code

 * adding a sequential number to a form

 add_filter("gform_pre_render", "process_unique");
function process_unique($form) {
    global $uuid;
    $uuid['form_id'] = 14;
    $uuid['field_id'] = 18;

    add_filter("gform_field_value_uuid", "get_unique");
    return $form;

function get_unique(){
    global $uuid;
	$form_id = $uuid['form_id'];
    $field_id = $uuid['field_id'];
    $prefix = "ALE-FOR-AUTISM - #";
    do {
		$formid = $form_id; //get ID of the form
		$form_count = RGFormsModel::get_form_counts($formid);
        $unique = $form_count['total'] + 1; // count of the lead form entries incremented by one
		$unique = str_pad($unique, 3, '0', STR_PAD_LEFT); // padding for number format 001,002...015 so 3 digit number format
        $unique = $prefix . $unique; // prefix and the unique number
    } while (!check_unique($unique, $form_id, $field_id));

    return $unique;

function check_unique($unique, $form_id, $field_id) {
    global $wpdb;

    $table = $wpdb->prefix . 'rg_lead_detail';
    $result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'");

        return true;

    return false;

How about if you populate the field as you are doing now, but hide it with conditional logic when the donation selection is under $20? Any field hidden by conditional logic when the form is submitted won’t be stored in the entry.

Thanks Chris. I did this and it works. I see a potential problem with my code though.
The odds of 2 users clicking submit at the exact same millisecond are remote especially on this low traffic site. BUT right now my code checks the DB and increments the number when the form loads.
So one user could open the form and mess about filling it in for a couple of minutes having received a incremented value but not having submitted it yet. Another user opens the form and it checks and gives back the same number!
Is there a way to run my code when the submit button is clicked, so just in that moment it fetches the last number from the DB and increments and saves?
I am no PHP guy, I managed to get some increment number code from the internet and hack it to my purpose, making it work was a big win for me. I figure i need add_action( 'gform_pre_submission_12', 'pre_submission' );
But I can’t wrap my code in it so that it works.

Hi @mgason. Yes, you could run into an issue if the number is generated when the form loads. If two people load the form before either one is submitted (and storing that value) you will have an issue with two people having the same code. It will happen eventually. You could do it yourself by opening the form in one tab, then opening the form in another tab. Both will have the same code.

You could work around that by checking to ensure the code is still unique, when the form is submitted, using the gform_pre_submission filter (checking to see if the code already exists in the database.) If it does, you would need to generate a new code at that point and change the code in the $_POST before the entry is stored. It’s a lot to think about buy necessary to prevent duplicate codes,

Could I pull the database submission entry number and add that to the email as the code. It will always be unique? Another thought.
I have tried wrapping my unique ID generating code in the presubmission filter and got nothing in the DB. My PHP skills not up to the job I guess. My idea was not to create the number until presubmission.
Any tips on wrapping my code in presubmission so it generates the code as it is submitted?

Sure. When do you want to do that? The entry ID will always be unique, but when are you trying to pull that and what are you trying to do with that?

I would need to pull it to put in the notification email to the donor and the notification to us.
So after conformation from Stripe. It does not need to show on the conformation page. Just in the emails
As there is a donor level which do not receive a gift code I would need to be able to say if formX-fieldX is not ‘no gift’ add DB entry ID to email

The entry ID is available with a merge tag in the notification. You can use this merge tag:

{entry:id} or the deprecated {entry_id}

That will put it into your Gravity Forms notification. No code required. In order to include that conditionally, use multiple notifications, and conditional logic on each to check if your field is “no gift” and use one notification or the other based on that. Here is information on setting up conditional notifications: