Sum of calculations field from 2 differents forms
-
Hello,
I have 2 differents forms, form A and form B and I would like to calculate the sum of 2 calculation fields of these forms.
Some people told me to use javascript but don’t know how to use it and I’m affraid that it doesn’t work anymore if any update of forminator come later.
Any idea how to make it work ?The page I need help with: [log in to see the link]
-
Hi @benech17
I hope you’re well today and thank you for your question!
I’m not sure if I understand that correctly. I see that there are two separate forms on the page.
Each of them has a calculation field so your goal is to calculate the sum of these two separate fields – add value of calculation field of one form and the other field?
If yes, then this isn’t something supported by Forminator. It might be possible with some custom code but what should actually be happening with that sum?
I mean: it’s only about displaying the value on the page (and doing nothing else with it except showing it there)? Or are you expecting it to be “re-used” in some way – if yes, what way?
Please advise and we’ll see what could be done about that.
Best regards,
AdamHello Adam,
Thanks for your answer ! Yes, it’s only about displaying the value on the page as a “rest “. form A is about income and form B is about expense so I would like to have the rest of it , as income minus expense.Hi @benech17
I hope you are doing good today.
I guess you could try the Pre-Populate feature
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#pre-populate-form-field-values
Note this will work for forms that exist on separate pages.Page 1 -> form 1 -> you calculate your income
You populate your calculation to page 2
Page 1 -> form 2 -> you store your calculation from form 1, you calculate the expense, then you calculate again as income minus expense.Is that the goal?
Kind Regards,
KrisHello Kris,
Not really. These forms need to be on the same page so the pre populate feature doesn’t seem to be a possible solution to my problem.
Thanks for helping !
I think Adam has well understood my idea.Hi @benech17
I hope you are doing well.
Could you please add an extra field on your second Form so we can help you with a JS code?
Best Regards
Patrick FreitasHello,
I’ve add HTML fields, is that what you wanted ?
Thanks for helping,
YanivHi @benech17
Can you please try this script as a mu-plugin?
<?php add_action('wp_footer', function(){ ?> <script> jQuery( document ).ready(function() { let calc_1 = jQuery('#forminator-module-704 #calculation-1-field'); let calc_2 = jQuery('#forminator-module-705 #calculation-1-field'); calc_1.on('change', listenToCalc ); calc_2.on('change', listenToCalc ); function listenToCalc() { let calc_1_val = parseFloat( calc_1.val() ); let calc_2_val = parseFloat( calc_2.val() ); let html_field = jQuery('#forminator-module-705 #html-2 h4'); let sum_calc = calc_1_val + calc_2_val; sum_calc = sum_calc.toFixed(2).replace('.',',').replace(' ',''); html_field.text("Reste : " + sum_calc + " €"); } }); </script> <?php }, 999 );
To install it as a mu-plugin please follow https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
The working video https://monosnap.com/file/LvMVkNj3PU6OgN8Jt8RgUzJJc0bVme
Best Regards
Patrick FreitasHello Patrick,
Thanks à lot for your contribution, it works very well for the values under 1000 but I found bugs when we want to use values over thousands. For example, 1250 will be catched as 1, surely due to the space between 1 and 250, as we display it.
So, 1 250 -> 1 .
999 -> 999 .Do you know how can we fix that ?
Best regards,
YanivHi @benech17
Can you try this updated version?
<?php add_action('wp_footer', function(){ ?> <script> jQuery( document ).ready(function() { let calc_1 = jQuery('#forminator-module-704 #calculation-1-field'); let calc_2 = jQuery('#forminator-module-705 #calculation-1-field'); calc_1.on('change', listenToCalc); calc_2.on('change', listenToCalc); function listenToCalc() { let calc_1_val = parseFloat(calc_1.val().replace(' ', '')); let calc_2_val = parseFloat(calc_2.val().replace(' ', '')); let html_field = jQuery('#forminator-module-705 #html-2 h4'); let sum_calc = calc_1_val + calc_2_val; sum_calc = sum_calc.toLocaleString('fr-FR', { style: 'currency', currency: 'EUR' }); html_field.text('Reste : ' + sum_calc ); } }); </script> <?php }, 999 );
Issue should be fixed now https://monosnap.com/file/JHOXZkznVLKFFCIYgZmFR14lkrXI6q
Best Regards
Patrick FreitasYeah it works very well now !
Thank you so much @wpmudevsupport12 !Best Regards,
Yaniv BenichouHi @benech17,
Glad to know that the solution works, and thank you. If you liked using our plugin, can you please quickly rate it? This will help us to keep the plugin available for free.
https://wordpress.org/plugins/forminator/#reviews
Best Regards,
Nebu John
- The topic ‘Sum of calculations field from 2 differents forms’ is closed to new replies.