Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
13

Form Calculation using Radio Buttons ?

Community Beginner ,
Jan 29, 2024 Jan 29, 2024

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?

 

Screenshot.gif

TOPICS
How to , PDF , PDF forms
811
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 29, 2024 Jan 29, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 29, 2024 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2024 Jan 29, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 29, 2024 Jan 29, 2024
LATEST

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines