Home › Forums › Quform PHP › Calculate Age from DOB
- This topic has 5 replies, 2 voices, and was last updated 8 years, 2 months ago by shorthills11.
- AuthorPosts
- September 5, 2016 at 5:40 am #19833shorthills11Participant
Hi,
I’m trying to calculate the age given the dob information entered, and store it in the $age variable. I’ve been able to get the values for the year, month, and day from the objects and perform my calculation.This issue: but when I try to add this new calculated $age variable to the $form, it throws an error:
“Catchable fatal error: Argument 1 passed to Quform::addElement() must be an instance of Quform_Element, string given, called in /var/www/public/content/ivf_treatments/quform/process2.php on line 263 and defined in /var/www/public/content/ivf_treatments/quform/lib/Quform.php on line 30”
Here is the relevant code:
$age = new Quform_Element('age', 'Age'); /* get year, month, day data from object */ $year = $form->getValue('dob_year'); $month = $form->getValue('dob_month'); $day = $form->getValue('dob_day'); /* create $dob time string */ $dob = strtotime("$year-$month-$day"); /* calculate age from $dob */ $age = floor((time() - strtotime($dob)) / 31556926); $age .= "variable test"; /* adding "variable test" to $age to test that it's working. */ $form->addElement($age); // TIHS IS LINE 263
Line 263 is the last line in the code.
Thank you in advance for any help!- This topic was modified 8 years, 2 months ago by shorthills11.
September 5, 2016 at 5:47 am #19835shorthills11ParticipantSo I did some more digging and I was able to get the form to process.
I’m developing locally so I have to test via SMTP, but this runs:/* $age = new Quform_Element('age', 'Age'); */ /* get year, month, day data from object */ $year = $form->getValue('dob_year'); $month = $form->getValue('dob_month'); $day = $form->getValue('dob_day'); /* create $dob time string */ $dob = strtotime("$year-$month-$day"); /* calculate age from $dob */ $age = floor((time() - strtotime($dob)) / 31556926); $age .= "variable test"; /* adding "variable test" to $age to test that it's working. */ $age = new Quform_Element('age'); // added this line $form->addElement($age);
Note I commented out the original $age declaration, and made a new one.
September 5, 2016 at 10:04 pm #19849shorthills11ParticipantOk so the form processes, butI can’t get the calculation to post in the email.
What I did was
a) I restored the original age section back to normal (though I pulled it off the .html end of the form)
b) I added the calculation code to the “custom code section #1”
c) I then used the advice at the URL below about how to add an item to the end of the form. I’m able to successfully show the field “Calculated Age” on the email. However, I can’t see the value of the $calcAge variable.Is this statement legal:
$config['extra']['Calculated Age'] = $calcAge;
September 5, 2016 at 10:09 pm #19850shorthills11ParticipantAnd to rule out a problem with my calculation code I added the $sample variable with some string data to the custom code section to see if I could get this data to appear on the emailed form.
// Custom code section #1 - see documentation for examples $sample = "this is sample data"; /* get year, month, day data from object */ .....
and then I tried to push the data to the form:
// Add the visitor IP to the email $config['extra']['IP address'] = Quform::getIPAddress(); $config['extra']['Calculated Age'] = $calcAge; // not showing up in the email $config['extra']['Sample Data'] = $sample; // not showing up in the email
Any help figuring this out would be greatly appreciated!
Thanks!September 9, 2016 at 9:29 am #19888AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
September 11, 2016 at 3:45 pm #19914shorthills11ParticipantThat works!
Thank you! - AuthorPosts
- You must be logged in to reply to this topic.