Navigation überspringen

[Gelöst] Help using cred_save_data hook / Option after sending form

This support ticket is created vor 6 Jahren, 1 Monat. 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

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

Zuletzt aktualisiert von Nigel vor 6 Jahren, 1 Monat.

Assistiert von: Nigel.

Author
Artikel
#1146490
Toolset_After_Sending_Form.jpg

Hi Nigel,

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

Thanks
Thorsten

#1146520

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Thorsten

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?

#1146543

Hi Nigel,

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.

#1146546

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.

#1146571

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Thorsten

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 (versteckter 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).

You would then use update_post_meta to store the value of the calculated age (https://developer.wordpress.org/reference/functions/update_post_meta/).

There are examples of using cred_save_data in the documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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.

#1146577

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.

Thanks
Thorsten

#1146600

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi, sorry about the second question.

The options are either/or options, and you cannot both display a message and reload the form.

I suggest you include a link in your form success message to essentially reload the page so that the form is displayed, ready to be submitted again.

On my local test site my form message looks like this:

Submitted, thanks

<a href="[referer]">Submit another</a>

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:

/**
 * Output URL
 */
add_shortcode('referer', function ($atts = [], $content = null) {

	return $_SERVER['HTTP_REFERER'];
});

(The wpv-post-url shortcode would output the URL of the newly created post, not the page where the form was hosted.)