Calculate Age from DOB

Home Forums Quform PHP Calculate Age from DOB

This topic is: not resolved
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #19833
    shorthills11
    Participant

    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.
    #19835
    shorthills11
    Participant

    So 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.

    #19849
    shorthills11
    Participant

    Ok 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;
    
    #19850
    shorthills11
    Participant

    And 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!

    #19888
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #19914
    shorthills11
    Participant

    That works!
    Thank you!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy