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

Slack Signup
Newsletter Optin
Help Desk

Calculations from checkboxes

Labels

This Discussion is public

Notifications

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

  1. Where would I place the PHP code in my form to do the calculations?
  2. 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
  3. 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.

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

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.