Skip to main content
mgover77
Participating Frequently
January 29, 2024
Answered

Form Calculation using Radio Buttons ?

  • January 29, 2024
  • 2 replies
  • 1030 views

I need to create a form to calculate the cost of an item after a discount is applied. However, the discount can be entered as either:

  • A flat dollar amount (price - discount = totalCost), or...
  • A percentage (discount/100 * price = totalCost

 

I'm thinking the best way to do this would be to add two radio buttons next to the DISCOUNT input field;

  • Radio 1 would be for a flat dollar discount
  • Radio 2 would be for a percentage discount

And then the totalCost would use an IF/ELSE statement to calculate using the proper formula.

 

But I have no idea how to write that calculation. Can anyone help?

 

This topic has been closed for replies.
Correct answer Thom Parker

You can read about writing calculation scripts and "if" statements here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

To make this work we have to know the names of the fields involved and the export values of the radio buttons. 

I'll just assume some names for the demo script, but you will need to modify it for the real names. 

 

if(this.getField("DiscountType").value == "FlatAmount")
  event.value = this.getField("Price").value - this.getField("Discount").value;
else
  event.value = this.getFeld("Price).value * (1 - this.getField("Discount").value/100);

 

 

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
January 30, 2024

You can read about writing calculation scripts and "if" statements here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

To make this work we have to know the names of the fields involved and the export values of the radio buttons. 

I'll just assume some names for the demo script, but you will need to modify it for the real names. 

 

if(this.getField("DiscountType").value == "FlatAmount")
  event.value = this.getField("Price").value - this.getField("Discount").value;
else
  event.value = this.getFeld("Price).value * (1 - this.getField("Discount").value/100);

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
mgover77
mgover77Author
Participating Frequently
January 30, 2024

Thank you Thom! This worked like a charm. I just cleaned up a few typos...

 

if(this.getField("DiscountType").value == "FlatAmount")
  event.value = this.getField("Price").value - this.getField("Discount").value;
else
  event.value = this.getField("Price").value * (1 - this.getField("Discount").value/100);

 

mgover77
mgover77Author
Participating Frequently
January 29, 2024

Correction:

I just realized my percentage calculation should have been... price - (discount/100)price = totalCost

 

Regardless, I need help writing the field script.