The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Set values to multiple fields
Hello,
I would like to add a couple of values to multiple fields.
First I check if the address that the user filled in is right and then I want to copy the Latitude and Longitude to a hidden field. Is it possible to only check ones the address and the copy the values to 3 different hidden fields?
/* Controleren of het opgegeven adres bestaat. (Formidable forms) */
add_filter('frm_validate_field_entry', 'adresgegevens', 10, 3);
function adresgegevens($errors, $posted_field, $posted_value){
$straat = 222; //Street
$huisnr = 224; //House number
$woonplaats = 225; //City
$land = 226; //Country
/* Make one field with the address that Google Maps can search */
if($posted_field->id == $straat){ //the ID of the field to validate
$adresEN = '';
$adresEN .= $_POST['item_meta'][$straat];
$adresEN .= ' ';
$adresEN .= $_POST['item_meta'][$huisnr];
$adresEN .= ', ';
$adresEN .= $_POST['item_meta'][$woonplaats];
$adresEN .= ', Netherlands';
/* Get the address information */
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($adresEN).'&sensor=false&language=nl&key=XXX');
$geo = json_decode($geo, true); // Convert the JSON to an array
/* Check the output of Google Maps */
if (isset($geo['status']) && ($geo['status'] == 'OK')) {
$adresCorrect = $adresEN;
$lat = $geo['results'][0]['geometry']['location']['lat']; // Latitude
$lon = $geo['results'][0]['geometry']['location']['lng']; // Longitude
} else if(isset($geo['status']) && ($geo['status'] == 'ZERO_RESULTS')) {
$adresCorrect = 'Het opgegeven adres is niet gevonden.';
if (is_user_logged_in()) {
$errors['field'. $straat] = 'Het opgegeven adres klopt niet: ' . $adresEN;
} else {
$errors['field'. $straat] = 'Het opgegeven adres klopt niet.';
}
$lat = '';
$lon = '';
} else {
$adresCorrect = 'Er is een fout opgetreden bij het opzoeken van het adres: ' . $geo['status'];
$lat = '';
$lon = '';
}
//Set the values to hidden fields
$_POST['item_meta'][233] = $lat;
$_POST['item_meta'][234] = $lon;
$_POST['item_meta'][235] = $adresCorrect;
}
/* Return an error if the address is not found */
return $errors;
}
Looking forward to receiving a reply,
Kind regards,
Joep Hoffland
Discussion closed.