[Resolved] Help using cred_save_data hook / Option after sending form
This support ticket is created 6 years ago. 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
in addition to an older thread (https://toolset.com/forums/topic/split-automatically-calculate-age-from-date-field/page/2/#post-1119145), I now want to input the calculated age of a person with your mentioned cred_save_data hook instead of pasting the calculated age from the custom jQuery code, because sometimes, the age is not calculated correctly (if the user uses the datepicker field wrong) and the age is 0 for the user. So I want to make sure that via the hook the age will be pasted in the Email message field. Actually I don't know how this works. Could you please give me some input on this?
Also is there a way that if the user filled out the form and clicked on the "Send"-button, that a message will be shown AND also the form well be cleared as well so the user can enter another registration for an event? Right now you have the chance to do only one of those options (see attached screen).
Please remind me, the user enters a date (date of birth?), and then an age field is updated via jQuery based upon the entered date of birth.
If users don't correctly enter the date of birth then the age is not calculated correctly and can show zero.
Instead of calculating the age with jQuery, your question is about calculating it on the server after the form has been submitted, using the cred_save_data hook.
My question then would be, if the date of birth has not been entered correctly, how is it possible to calculate the age, even on the server?
Or is the issue that the date of birth is entered correctly, but because of some fat-finger problem the jQuery update of the age field fails, even though the date of birth is correct?
yes, you remind correctly. In some cases, the age was 0 even if the the age was shown correctly. I tried to reproduce this issue but could not reproduce it UNTIL now. I tried it over my laptop (using chrome): If you set a birthdate in the datepicker, the age will be calculated correctly. BUT if you click again in the date-field, the age will be set to 0 and the birth-date is still correct. So that could be, what the users have done, for which the 0 was shown as calculated age with correct set birthdate (see attached screenshot).
So I think, there could be something wrong with the jQuery code and/or the event which will be triggered when enter the date field.
Also is there a way that if the user filled out the form and clicked on the "Send"-button, that a message will be shown AND also the form well be cleared as well so the user can enter another registration for an event? Right now you have the chance to do only one of those options.
In addition to my last post (#1146543) I wasn't able to upload an image. I cleared cache of chrome and firefox as well, restaretd the browsers and still getting HTTP error when trying to upload an image. I also wasn't be able to edit my last post.
If there is a problem with the jQuery in the original solution you may need to review to see if you can identify why: our support policy doesn't include providing custom code precisely because we then find we need to support it (rather than supporting the plugins and APIs themselves). To the extent that I helped with that code in the earlier thread you should consider it as a starting point that you may need to finesse.
If you want to calculate the age on the server then the way this would work is that your form will be submitted with a value for the date of birth field.
You can trigger PHP code with the cred_save_data hook, which fires when the form has been processed and the post and related custom fields have been saved. So at this point the date of birth can be retrieved using get_post_meta (https://developer.wordpress.org/reference/functions/get_post_meta/), and this will be a timestamp. You can similarly get the start date of the event, and you can use the PHP date_diff function (hidden link) to get the difference between the two dates in years (i.e. the age at the time of the event; you may have to convert the dates to the same format before comparing).
I have reported the issues you are having with the forum (uploading images, editing replies) to our systems team which they are looking into, thanks for raising them.
Hi Nigel,
I will try this the next days but I think, that this is doeable as I know some PHP.
You forgot to answer my second question:
Is there a way that if the user filled out the form and clicked on the "Send"-button, that a message will be shown AND also the form well be cleared as well so the user can enter another registration for an event? Right now you have the chance to do only one of those options.
I had to register a custom shortcode—referer—to generate the link to essentially reload the same page minus the URL parameters used to trigger display of the message, like so: