The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Calculations from checkboxes
I am creating a form in which I will be counting the number of checkboxes that are checked in order to do some calculations. Here's what I'm doing.
The item will display a number of classes for a user to select. Those classes selected will be passed on in the email. Based on the number of classes checked, I can count those to do my calculations. The first class will cost $20 and every one after that will be $10. So my questions are...
- Where would I place the PHP code in my form to do the calculations?
- Is my code for calculating correct? (I am assuming I use the name field and not the field key)
$howMany = sizeof ( $_REQUEST[ 'classes' ] ); //count the number of checkboxes checked
$total = 20 + (($howMany - 1) * 10)); //total fees
- How and where do I assign the "$total" variable to a hidden form element i.e. [amount] to be sent in the email? Can I still use PHP and add this to the code above?
$_POST['amount'] = $total;
This way I can use "[amount]" when crafting my message and for sending the amount to Paypal.
Thanks for your help.
August 15, 2017 at 5:15 pm
You may be better off using jQuery to calculate the form in real-time and store the values in hidden fields. Here's an example of how it can work: https://codepen.io/Mestika/pen/aitHh
August 15, 2017 at 11:21 pm
Thanks Victor.
I was able to accomplish what I needed. Also found this in the docs https://formidableforms.com/knowledgebase/javascript-examples/#kb-count-number-of-checked-boxes for those who may need something similar.
Discussion closed.