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

Using Calculate function And The Text Only Prints when Field Boxes are Filled

Explorer ,
Oct 31, 2023 Oct 31, 2023

Hello all,

 

I am still in the process of perfecting an expense sheet for my job.  I would like for the grand total to self calculate when the form fields have data entered into them.  However, if no data is entered and the user just wants to print a form to hand write their expenses I would like for the Grand Total to NOT print.  How do I go about this? I have tried visible but doesn't print and then the calculated total doesn't appear.  I have tried print but hidden and that makes it print out a 0 when no data is entered and you can't see the total on the sheet when using it on a computer.

 

I attached the document for you to see and a .jpg explaining what I am asking to learn how to do.  I appreciate your time and thank you.

 

J

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF , PDF forms
1.2K
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
Adobe Employee ,
Oct 31, 2023 Oct 31, 2023

Hi @johnsbox 

 

Hope you are doing well and thanks for reaching out.

 

To achieve the behavior you described, you can use JavaScript to dynamically calculate and display the grand total when data is entered into form fields and hide it when no data is entered. Here's a sample JavaScript code for this purpose

Note: I am not an expert on JavaScript, Please check the code below and see if that helps.

 

<!DOCTYPE html>
<html>
<head>
<title>Expense Form</title>
</head>
<body>
<form id="expenseForm">
<label for="expense1">Expense 1:</label>
<input type="number" id="expense1" class="expenseInput"><br>

<label for="expense2">Expense 2:</label>
<input type="number" id="expense2" class="expenseInput"><br>

<label for="expense3">Expense 3:</label>
<input type="number" id="expense3" class="expenseInput"><br>

<label for="grandTotal">Grand Total:</label>
<span id="grandTotal"></span><br>

<button type="button" id="calculateButton">Calculate</button>
</form>

<script>
// Get references to form fields and the grand total element
const expenseInputs = document.querySelectorAll('.expenseInput');
const grandTotalElement = document.getElementById('grandTotal');
const calculateButton = document.getElementById('calculateButton');

// Add a click event listener to the Calculate button
calculateButton.addEventListener('click', calculateGrandTotal);

function calculateGrandTotal() {
let total = 0;

// Calculate the total by summing the values of filled input fields
expenseInputs.forEach(input => {
if (input.value) {
total += parseFloat(input.value);
}
});

// Display the grand total if it's greater than zero, hide it otherwise
if (total > 0) {
grandTotalElement.textContent = total;
} else {
grandTotalElement.textContent = '';
}
}

// Initially, hide the grand total
calculateGrandTotal();
</script>
</body>
</html>

 

This code creates an expense form with three input fields for expenses, a grand total field, and a Calculate button. When you click the Calculate button, it sums the values of the filled input fields and displays the grand total. If the grand total is zero or no data is entered, it hides the grand total.

 

~Amal

Regards
Amal
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 ,
Oct 31, 2023 Oct 31, 2023

This code is way off. It's not even valid Acrobat JS code.

It's better not to post code at all, than to post incorrect code.

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 ,
Oct 31, 2023 Oct 31, 2023
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 ,
Nov 01, 2023 Nov 01, 2023
LATEST

You can use this as a Custom Format script, if the field value is zero, it is not displayed (and not printed):

 

if (event.value && !isNaN(Number(event.value)) && event.value != 0) {AFNumber_Format(2, 0, 0, 0, "$ ", true);}
else {event.value = "";}

 

See attachment.


Acrobate du PDF, InDesigner et Photoshoptographe
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