• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Form Calculation using Radio Buttons ?

# Form Calculation using Radio Buttons ?

Community Beginner ,
Jan 29, 2024 Jan 29, 2024

Copied

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?

TOPICS
How to , PDF , PDF forms

Views

278

Report

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

Community Expert , Jan 29, 2024 Jan 29, 2024

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

3 Replies 3
Community Beginner ,
Jan 29, 2024 Jan 29, 2024

Copied

Correction:

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

Regardless, I need help writing the field script.

Report

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

Copied

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

Report

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

Copied

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