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.
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.
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 ) {).
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.
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;
}