This topic is locked

Edit As Setting: Default Value that calculates Age

6/18/2010 1:57:50 PM
PHPRunner General questions
J
jackieh author

We have a form with a 'Date of Birth' and an 'Age' field. When the 'Date of Birth' is entered we want the 'Age' field to automatically show the age based on the date of birth. How should we do that? Can we put a calculation in 'Edit As Setting' 'Default Value'? What would be the correct syntax? Thank you very much.

A
ann 6/22/2010

Hi,
use Before record added event on the Events tab. Here is a sample:

$dob_year=date("Y", strtotime($values["date_of_birth"]));

$dob_month = date("n", strtotime($values["date_of_birth"]));



$today_year = date("Y", strtotime(date("Y-m-d")));

$today_month = date("n", strtotime(date("Y-m-d")));
$age=$today_year - $dob_year;

if ($dob_month>$today_month) {$age-=1;}
$values["age"]=$age;


where date_of_birth is actual 'Date of Birth' field name, age is actual 'Age' field name.

Don't forget to uncheck 'Age' field for the add page on the Choose fields tab.

J
jackieh author 6/22/2010

Hi Ann,
Thanks very much for your response. The user of the form needs to see the age value in the Age field as they are using the form. If it is calculated Before Record Added they never see the Age value.
So I was wondering if the Age field can be a calculation as a default value so once the user enters the Date Of Birth, the Age field gets populated on the form right away. Is that possible?
Thanks again.



Hi,
use Before record added event on the Events tab. Here is a sample:

$dob_year=date("Y", strtotime($values["date_of_birth"]));

$dob_month = date("n", strtotime($values["date_of_birth"]));
$today_year = date("Y", strtotime(date("Y-m-d")));

$today_month = date("n", strtotime(date("Y-m-d")));
$age=$today_year - $dob_year;

if ($dob_month>$today_month) {$age-=1;}
$values["age"]=$age;


where date_of_birth is actual 'Date of Birth' field name, age is actual 'Age' field name.

Don't forget to uncheck 'Age' field for the add page on the Choose fields tab.

A
ann 6/24/2010

Hi,
to add values on the fly use JavaScript API (JavaScript onload event on the Events tab).

Here is a sample:

var ctrlAge = Runner.getControl(pageid, 'age_field');

var ctrlDate = Runner.getControl(pageid, 'date_of_birth');

now = new Date();

function func() {

var dateofbirth=new Date(ctrlDate.getValue());

var age=now.getFullYear()-dateofbirth.getFullYear();

if (now.getMonth()<dateofbirth.getMonth()) {

age=age-1;

}

ctrlAge.setValue(age);

};
ctrlAge.on('click', func);



here age_field, date_of_birth are actual age, date of birth field names.

J
jackieh author 6/24/2010

Thank you so much Ann!



Hi,
to add values on the fly use JavaScript API (JavaScript onload event on the Events tab).

Here is a sample:

var ctrlAge = Runner.getControl(pageid, 'age_field');

var ctrlDate = Runner.getControl(pageid, 'date_of_birth');

now = new Date();

function func() {

var dateofbirth=new Date(ctrlDate.getValue());

var age=now.getFullYear()-dateofbirth.getFullYear();

if (now.getMonth()<dateofbirth.getMonth()) {

age=age-1;

}

ctrlAge.setValue(age);

};
ctrlAge.on('click', func);



here age_field, date_of_birth are actual age, date of birth field names.