I am trying to dynamically populate a field called "Age" with the age of the person filling out the form based on the date of birth they already filled out.
This is on a multi-page form. The DOB field is a date field on the 1st page.
I placed the "Age" field on the second page so the DOB field will have to validate before seeing the "Age" field. It is important that this happens on validation, not on submission as I have a lot of conditional logic based on age.
Here is my code which I placed in functions.php:
add_filter("gform_pre_submission_filter_2", "age");
function age($form){
$ageTime = strtotime($_POST["input_5"]); // Get the person's birthday timestamp
$t = time(); // current time
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$age= floor($ageYears);
$form["fields"][6]["defaultValue"] = $age; //set default value of Age field to the calculated age
return $form;
}
Nothing is showing up in the Age field when I run this at this point. What am I doing wrong?