Skip to main content
Participating Frequently
January 10, 2025
Answered

How to manually override a calculation fillable forms

  • January 10, 2025
  • 3 replies
  • 1221 views

I can auto-calculate the miles in my fillable form using the table and script below. However, I want them to be able to select an "other" as an option in the drop-downs (putting an explanation in the purpose column) so they can manually input their own miles in the miles column. How can I do that? 

 

var Loc1=this.getField("Location1").value;
var Loc2=this.getField("Location2").value;
if((Loc1=="Sports Center" && Loc2=="Transportation")||(Loc1=="Transportation" && Loc2=="Sports Center"))
{event.value=3.7} else
if((Loc1=="Sports Center" && Loc2=="Middle School")||(Loc1=="Middle School" && Loc2=="Sports Center"))
{event.value=2.3} else
if((Loc1=="Sports Center" && Loc2=="Senior High")||(Loc1=="Senior High" && Loc2=="Sports Center"))
{event.value=2.0} else
if((Loc1=="Sports Center" && Loc2=="Education Center")||(Loc1=="Senior High" && Loc2=="Sports Center"))
{event.value=3.1} else
if((Loc1=="Sports Center" && Loc2=="Central Learning Center/STEP")||(Loc1=="Central Learning Center/STEP" && Loc2=="Sports Center"))
{event.value=3.1} else

 

etc... 

 

 

Correct answer Thom Parker

Is this what you mean? It doesn't work. I'm very new to this, so understanding takes a bit. Thank you for being so patient and helping me. 

var Loc1=this.getField("Location1").value;
var Loc2=this.getField("Location2").value;
event.rc = Loc1 != "OTHER";
event.rc = Loc2 != "OTHER";
event.rc = (Loc1 != "OTHER") || (Loc2 != "OTHER");
if((Loc1=="Sports Center" && Loc2=="Transportation")||(Loc1=="Transportation" && Loc2=="Sports Center"))
{event.value=3.7} else
if((Loc1=="Sports Center" && Loc2=="Middle School")||(Loc1=="Middle School" && Loc2=="Sports Center"))
{event.value=2.3} else
if((Loc1=="Sports Center" && Loc2=="Senior High")||(Loc1=="Senior High" && Loc2=="Sports Center"))
{event.value=2.0} else


Use this code:

var Loc1=this.getField("Location1").value;
var Loc2=this.getField("Location2").value;
event.rc = (Loc1 != "OTHER") || (Loc2 != "OTHER");

if((Loc1=="Sports Center" && Loc2=="Transportation")||(Loc1=="Transportation" && Loc2=="Sports Center"))
    {event.value=3.7} 
else if((Loc1=="Sports Center" && Loc2=="Middle School")||(Loc1=="Middle School" && Loc2=="Sports Center"))
    {event.value=2.3} 
else if((Loc1=="Sports Center" && Loc2=="Senior High")||(Loc1=="Senior High" && Loc2=="Sports Center"))
    {event.value=2.0} 

 

The code must not end with a hanging "else". This will cause an error that will prevent the code from running. 

Also, "OTHER" must match the selection text exactly, including case. 

 

If the code doesn't work there may be an error somewhere. Errors are reported in the Console Window. Press (Ctrl-J).

Good Luck,

 

3 replies

Thom Parker
Adobe Expert
January 10, 2025

The form could also benefit from proper table naming. This would allow you to generalize the scripts. 

For example "Row1.Location1",  "Row2.Location1", etc. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Adobe Expert
January 10, 2025

There are several ways to block a calculation. 

But the simplest is to use the event.rc value in the calculation script. 

Add this line to the top of the miles calculation code.

 

event.rc = Loc2 != "Other";

 

this could also be an "if" condition if you want to keep the calculation from proceeding. The only reason to do this is if the calculation takes time or affects other fields. 

 

if(event.rc = (Loc1 != "Other))

{

    full calculation script

}

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
lori_9412Author
Participating Frequently
January 10, 2025

This worked! Thank you - 

What could I do if they select OTHER in the Destination From column but select one of the dropdown options in the Destination To column. It won't allow me to override the miles. It only works if they select "OTHER" in both the From and To column.

Thom Parker
Thom ParkerCorrect answer
Adobe Expert
January 11, 2025

Is this what you mean? It doesn't work. I'm very new to this, so understanding takes a bit. Thank you for being so patient and helping me. 

var Loc1=this.getField("Location1").value;
var Loc2=this.getField("Location2").value;
event.rc = Loc1 != "OTHER";
event.rc = Loc2 != "OTHER";
event.rc = (Loc1 != "OTHER") || (Loc2 != "OTHER");
if((Loc1=="Sports Center" && Loc2=="Transportation")||(Loc1=="Transportation" && Loc2=="Sports Center"))
{event.value=3.7} else
if((Loc1=="Sports Center" && Loc2=="Middle School")||(Loc1=="Middle School" && Loc2=="Sports Center"))
{event.value=2.3} else
if((Loc1=="Sports Center" && Loc2=="Senior High")||(Loc1=="Senior High" && Loc2=="Sports Center"))
{event.value=2.0} else


Use this code:

var Loc1=this.getField("Location1").value;
var Loc2=this.getField("Location2").value;
event.rc = (Loc1 != "OTHER") || (Loc2 != "OTHER");

if((Loc1=="Sports Center" && Loc2=="Transportation")||(Loc1=="Transportation" && Loc2=="Sports Center"))
    {event.value=3.7} 
else if((Loc1=="Sports Center" && Loc2=="Middle School")||(Loc1=="Middle School" && Loc2=="Sports Center"))
    {event.value=2.3} 
else if((Loc1=="Sports Center" && Loc2=="Senior High")||(Loc1=="Senior High" && Loc2=="Sports Center"))
    {event.value=2.0} 

 

The code must not end with a hanging "else". This will cause an error that will prevent the code from running. 

Also, "OTHER" must match the selection text exactly, including case. 

 

If the code doesn't work there may be an error somewhere. Errors are reported in the Console Window. Press (Ctrl-J).

Good Luck,

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Inspiring
January 10, 2025

If user don't use dropdowns (Loc1 and Loc2) to input miles, you can use event.source like this:

if (event.source && (event.source.name=="Location1" || event.source.name=="Location2")) {
//put your script here between blue curly brackets
}

 

It will update the calculation only when dropdowns are interacted with.