Navigation überspringen

[Gelöst] Calculate age of person at the beginning of an event

This support ticket is created vor 5 Jahren, 3 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 3 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 5 Jahren, 3 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1348093

Hi,
last year I had some toipics with Nigel regarding the calculating of the age of a person at the beggining of an event, for which this person registered. Until now I used a datefield and a custom solution from Nigel to calculate that age. But there are sometimes problems with this jQuery solution, because for some reason the age is 0 for some persons. Now I want to use three textfields for the users, where they can input their birthdate day, birthdate month and birthdate year. Based on their input I want to calculate the age of the person at the beginning of the event but after submitting the form field. Please see also topic https://toolset.com/forums/topic/help-using-cred_save_data-hook-option-after-sending-form/#post-1146571
Is there an easier way to realize that?
Thanks
Thorsten

#1348285

I think the better solution is to figure out why the age is sometimes 0 given the existing date field, and fix that problem, rather than introducing a whole other set of problems by adding three more input fields. If necessary you can use the cred_form_validate hook to check the value of the birthday date field, and display a message and error if the value is not set appropriately.

#1348333

Hi Christian,
the age of 0 problem only occurs sometimes. I couldn't reproduce the problem, because it works fine from my side. The solution now is a custom jquery-solution where the age will be calculated live after picking the dates from the datepicker. So if I understood you right, the cred_form_validate hook validates the age from server-side, right? If yes, do I have to put this hook into the custom code section in the Toolset settings? The calculation has to similar like in the jQuery code I guess?

 // Edit fields as required
    var dateField = 'geburts-datum';
    var ageField = 'alter-neu';
    var eventSelector = '#veranstaltungs-beginn';

    // Get current datepicker options
    var currentOptions = $('input[name="wpcf-' + dateField + '[display-only]"]').datepicker('option', 'all');

  	// Set new option for function to calculate age and update age field
    var newOptions = {
      yearRange: "1900:2050",

        onClose: function(dateText, instance) {

            if (instance.currentYear == 0) return;

            // get event start date
            var eventStart = $(eventSelector).text();
            var eventStartInt = parseInt(eventStart);
            var eventDate = new Date(eventStartInt * 1000);
            var eventYear = eventDate.getFullYear();
            var eventMonth = (eventDate.getMonth()) + 1;
            var eventDay = eventDate.getDate();

            var selectedYear = instance.selectedYear;
            var selectedMonth = (instance.selectedMonth) + 1;
            var selectedDay = instance.selectedDay;
            var age = eventYear - selectedYear;

            if (selectedMonth > eventMonth) age--;
            else {
                if (selectedMonth == eventMonth) {
                    if (selectedDay > eventDay) age--;
                }
            }
            // update target field with age
            $('input[name="wpcf-' + ageField + '"]').val(age);
            $("#alter-anzeige").text(age).appendTo("#alter-veranstaltungsbeginn");
			//$("#alter-anzeige").appendTo("#alter-veranstaltungsbeginn");
        }
    };
#1348469

So if I understood you right, the cred_form_validate hook validates the age from server-side, right? If yes, do I have to put this hook into the custom code section in the Toolset settings?
Yes the cred_form_validate hook is triggered on the backend when the form is submitted. You may put your PHP code in the custom code section of Toolset > Settings, or you may put the code in your child theme's functions.php file. However, if you are already using jQuery to perform front-end validation and form input manipulation, it may not be necessary. You might be able to add your own validation to the custom JS code.