Using input from one field to populate another

When someone selects a COUNTRY I would like to store a “Region” in another field. For example, if they select “France” the region field would be stored with “Europe.” This doesn’t need to be seen by them or appear on the form, but can happen after they submit the form.

I’m thinking it would work using gform_pre_validation.

Yes? No? Is there a better way?

THANKS

I have done things like this using the gform_pre_submission filter:

Thanks! That looks like just what I need.

And it’s good to see you around, Chris! You’ve been one of my best guides and pointers-in-the-right-direction for years.

Mad Dog

1 Like

Thank you! I’ll leave this open in case you have any questions about the code or if you want to share what you’ve done.

1 Like

I haven’t fully tested this in real life since I need to set up a User Registration Feed which comes next to make sure it works properly with the form, but in general PHP testing it works.

Unfortunately it’s not quite working…for some reason at the end, it won’t save the variable $region. It will take a string. But not that variable. Do I need to do something to the string or is my syntax off to do that? Seems straightforward but…

Other than that fix, does it look okay? The client wants several countries to be listed as regions (don’t ask) and of course I’ll be adding the rest of the long country list as soon as the client sorts it for me. Is there a faster/better way to do the arrays and searching, especially since there will be a full country list involved? PHP is not my #1 strong suit. Any suggestions are appreciated!

Thanks!
Mad Dog

// TO CHANGE REGION FIELD BASED ON INPUT IN COUNTRY FIELD
// Based on https://docs.gravityforms.com/gform_pre_submission/

//$country = "Nigeria";  //This is the value of the Country Field FOR TESTING

//USE ON FORM 6 ONLY
add_action( 'gform_pre_submission_6', 'pre_submission_handler_region' );
function pre_submission_handler_region( $form ) {

//DEFINE COUNTRY/REGION ARRAYS
$samecountry = array("United States", "Thailand", "Canada");
$asia = array("Japan", "China", "Laos");
$europe = array("France", "Germany", "Austria");
$latinamerica = array("Mexico", "Argentina", "Peru");
$middleeast = array("Israel", "Egypt", "Sudan");
$africa = array("Chad", "Niger", "South Africa");
$oceana = array("Australia", "New Zealand", "Mauritana");

// CONVERT INTO APPROPRIATE REGION AND SAVE IN VARIABLE $region
switch ($country) {
    case (in_array($country, $samecountry)):
        $region = $country;
        break;
    case (in_array($country, $asia)):
        $region = "Asia";
        break;
    case (in_array($country, $europe)):
        $region = "Europe";
        break;
    case (in_array($country, $latinamerica)):
        $region = "Latin America";
        break;
    case (in_array($country, $middleeast)):
        $region = "Middle East";
        break;
    case (in_array($country, $africa)):
        $region = "Africa";
        break;
    case (in_array($country, $oceana)):
        $region = "Oceana";
        break;
    default:
        $region = "Not listed";
}

//echo "The region is: ", $region;

// SAVE $region into REGION field

$_POST['input_12'] = $region;
}

P.S. It’s stripping out indents and I don’t see tags to use to preserve the code formatting in this editor.

You can use three backticks on one line to start a code block, if you like, or a single backtick for an inline snippet of code.

<?php
echo "Hello World!";

That was with three backticks, but you can use one backtick to get the code inline <?php echo "like this.\r\n";

This is what it looked like in the editor.

1 Like

Good to know.

Now…can you see why this code isn’t replacing that input field with the variable $region? If I try to replace it with a ‘string’ it works fine, but not the variable…

And second…is there a slicker, faster, better way to do these arrays?

And now as a tick check, here’s the code:

// TO CHANGE REGION FIELD BASED ON INPUT IN COUNTRY FIELD
// Based on https://docs.gravityforms.com/gform_pre_submission/

//$country = "Nigeria";  //This is the value of the Country Field FOR TESTING

//USE ON FORM 6 ONLY
add_action( 'gform_pre_submission_6', 'pre_submission_handler_region');
function pre_submission_handler_region( $form ) {

//DEFINE COUNTRY/REGION ARRAYS
$samecountry = array("United States", "Thailand", "Canada");
$asia = array("Japan", "China", "Laos");
$europe = array("France", "Germany", "Austria");
$latinamerica = array("Mexico", "Argentina", "Peru");
$middleeast = array("Israel", "Egypt", "Sudan");
$africa = array("Chad", "Niger", "South Africa");
$oceana = array("Australia", "New Zealand", "Mauritana");

// CONVERT INTO APPROPRIATE REGION AND SAVE IN VARIABLE $region
switch ($country) {
    case (in_array($country, $samecountry)):
        $region = $country;
        break;
    case (in_array($country, $asia)):
        $region = "Asia";
        break;
    case (in_array($country, $europe)):
        $region = "Europe";
        break;
    case (in_array($country, $latinamerica)):
        $region = "Latin America";
        break;
    case (in_array($country, $middleeast)):
        $region = "Middle East";
        break;
    case (in_array($country, $africa)):
        $region = "Africa";
        break;
    case (in_array($country, $oceana)):
        $region = "Oceana";
        break;
    default:
        $region = "Not listed";
}

//echo "The region is: ", $region;

// SAVE $region into REGION field
$_POST['input_35'] = $region;

}

I tried adding return $form; at the end but that didn’t help.

Right now, you are not defining $country anywhere, unless you set it in the string. If $country is a field in the form, you will need to grab the value from the $_POST and assign it to $country. For example, if your country came from field 23 in the form, you can add this in place of setting the string:

$country = rgpost( 'input_23', true );

That would get the value from the form submission field 23 and assign it to $country. Then you can use that in your switch/case statement. Move the assignment of the variable to inside your function (right after function pre_submission_handler_region( $form ) {).

Also, you do not need to return $form;.

1 Like

Well duh, freakin’ duh!

THANKS. I actually had a comment in the code earlier when I was testing it as straight PHP that I needed to get and store $country but obviously forgot about it completely.

Works a charm now!

Thanks for your help. I love when these things actually work. :sunglasses:

P.S. I’m sooooo glad forums are back!

1 Like

And just to be totally obnoxious, in case it does anyone else any good, here’s the working version. Of course it will need more countries in the region arrays…

// TO CHANGE A REGION FIELD BASED ON INPUT IN COUNTRY FIELD
// Based on https://docs.gravityforms.com/gform_pre_submission/

//USE ON FORM 6 ONLY
add_action( 'gform_pre_submission_6', 'pre_submission_handler_region');
function pre_submission_handler_region( $form ) {

// Grab country field and create variable
$country = rgpost( 'input_30', true );

//DEFINE COUNTRY/REGION ARRAYS
$samecountry = array("United States", "Thailand", "Canada");
$asia = array("Japan", "China", "Laos");
$europe = array("France", "Germany", "Austria");
$latinamerica = array("Mexico", "Argentina", "Peru");
$middleeast = array("Israel", "Egypt", "Sudan");
$africa = array("Chad", "Niger", "South Africa");
$oceana = array("Australia", "New Zealand", "Mauritana");

// CONVERT INTO APPROPRIATE REGION AND SAVE IN VARIABLE $region
switch ($country) {
    case (in_array($country, $samecountry)):
        $region = $country;
        break;
    case (in_array($country, $asia)):
        $region = "Asia";
        break;
    case (in_array($country, $europe)):
        $region = "Europe";
        break;
    case (in_array($country, $latinamerica)):
        $region = "Latin America";
        break;
    case (in_array($country, $middleeast)):
        $region = "Middle East";
        break;
    case (in_array($country, $africa)):
        $region = "Africa";
        break;
    case (in_array($country, $oceana)):
        $region = "Oceana";
        break;
    default:
        $region = "Not listed";
}

// SAVE $region into REGION field
$_POST['input_35'] = $region;

}

1 Like