SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDF Javascript - Question about Check boxes and calculations

    Hello Everyone,

    I have a PDF form. For the sake of simplicity, Lets say I have 5 fields. 2 Are check boxes (IDs FSA1 and FSA2), 2 are text fields (IDs TotalClaimed1 and TotalClaimed2), and a text field (FSA_Total) which adds the number from TotalClaimed1 if FSA1 if checked, and also add TotalClaimed2 is FSA2 is checked.

    My question is what I write in the "calculate custom script" box of the FSA_Total field: (I unsuccessfully tried)

    if (FSA1.checked == 1) {

    event.value += Totalclaimed1;

    }

    if (FSA2.checked == 1) {

    event.value += Totalclaimed2

    }

    I can post the actual form I am working on, if you think that will help. Just let me know

  2. #2
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yea, I might need to see the form because I'm not sure I think I understand what you need.

    I'm assuming you got FSA1 like this:
    var FSA1 = document.getElementByID('FSA1');

    I'm not sure what the "calculate custom script" box is though...

  3. #3
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello OMGCarlos,

    I've posted my TEST FORM.PDF to http://www.mediafire.com/?9dezannp7b1bj9z

    If you load it up, there are tons of fields and such - but to troubleshoot the issue, Lets focus on the first 2 lines of the claim form, and the box at the bottom displaying the FSA total.

    The box at the bottom, FSA_Total, should show 0. If the box FSA1 is checked, it should add the total from the field TotalClaimed1. Additionally, if the box FSA2 is checked, it should add the total from the field TotalClaimed2.

    (Once I can get that to work, modifying the code to apply to all 11 lines/4 benefits in the claim form should be relatively easy. )

    In Adobe Acrobat X, if you double click on FSA_Tota, you get the "Text Field Properties" box. Under the "Calculate" tab, the third option is a custom calculation script.

    This is the area where my JavaScript code is.

    if (FSA1.checked == 1) {

    event.value += Totalclaimed1;

    }

    if (FSA2.checked == 1) {

    event.value += Totalclaimed2

    }

    ------------

    Once you've had a chance to take a look at the PDF, if you are able to assist me with the code, I'll be incredibly greatful. (I just wish my company realized there is a difference between a graphic designer and a programmer!!!)

    BDavis

  4. #4
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's a live demo using your ID names: http://jsfiddle.net/smyeb/

    I'll be honest with you, I didn't know you could embed HTML into a PDF like this!

    I actually meant the form code, that's my fault. I'm on a Mac and don't have Acrobat Reader, so I can see the form but not the code.

    The code you copy+pasted looks like it should work (other than the fact that there's a missing ; after Totalclaimed2), but without knowing how "event" is set, how FSA1 is set, etc I can't really help out.

    Know that if you want get the checked state of a checkbox with, say, ID "Test" this won't work:
    Code:
    if(Test.checked == 1)
    You first need to do this:
    Code:
    Test = document.getElementByID('Test');
    if(Test.checked == 1)
    That would work.

  5. #5
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculating via if/then checkboxes

    Hello OMGCarlos,

    I took a look at the live JS page you mage. That was quite handy. I was trying to accomplish this without a calculate button - I am not sure if that's possible.

    Here is the code I'm using so far:

    var a=this.getField("TotalClaimed1");
    var b=this.getField("TotalClaimed2");
    var fsabox=this.getField("FSA_Total");

    if (this.getField("FSA1").value === "Yes") {
    fsabox.value =+ a.value;
    }

    if (this.getField("FSA2").value === "Yes") {
    fsabox.value =+ b.value;
    }

    fsabox.value
    -------------------------------

    But there is a problem with the code. Here is what I want to happen:
    If FSA1 is checked, add the value from TotalClaimed1 (var a) to the variable fsabox
    AND
    If FSA2 is checked, add the value from TotalClaimed2 (var b) to the variable fsabox

    That is not what's happening though. Here is what's happening instead:
    If FSA1 is checked, add the value from TotalClaimed1 (var a) to the variable fsabox
    BUT
    If FSA2 is checked, add the value from TotalClaimed2 (var b) to the variable fsabox instead.

    (If FSA2 is checked, fsabox only displays the amount from TotalClaimed2. It disregards the amount from TotalClaimed1, even if FSA1 is checked)

    Can you see what in the code is causing it to behave like this?

    Thanks

  6. #6
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You've got your operators flipped, should be:
    +=
    not
    =+

    The former adds/concatenates to an existing number/string while the latter...does something weird.

  7. #7
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flipping the operators caused "something weird" to happen.
    I changed the code, and here is what happened:

    I checked FSA1, and put $10 in TotalClaimed1. FSA_Total correctly showed $10.
    When I check FSA2, FSA_Total instantly changes to $1010 (incorrect)
    If I put a figure like $50 into TotalClaimed2, FSA_Total changes to 10101050

    So flipping the operators did not work.

    I very much appreciate your quick replies and troubleshooting with code. I am sure its not easy!

  8. #8
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    var claim1 = parseInt(this.getField('TotalClaimed1').value),
        claim2 = parseInt(this.getField('TotalClaimed1').value),
        fsabox = parseInt(this.getField("FSA_Total").value);
    
    if( this.getField('fsa1').value == 'Yes')
        fsabox += claim1;
    if( this.getField('fsa2').value == 'Yes')
        fsabox += claim2;
    
    fsatotal.value = fsabox;​

  9. #9
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh, no. Not entering the dollar sign. My apologies. That should have read:

    I checked FSA1, and put 10 in TotalClaimed1. FSA_Total correctly showed 10.
    When I check FSA2, FSA_Total instantly changes to 1010 (incorrect)
    If I put a figure like 50 into TotalClaimed2, FSA_Total changes to 10101050

  10. #10
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oops, updated the code after you posted.

    Either something funky is going on at your end, or I didn't get any sleep. Are you sure that's all the code? This should have been fixed a while ago lol

    Basically what's happening is your "adding" strings together, not numbers. Just use parseInt()

  11. #11
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your code. I had to change FSA to caps, so it recognized my fields. and the last line, I changed fsatotal.value to event.value.

    var claim1 = parseInt(this.getField('TotalClaimed1').value),
    claim2 = parseInt(this.getField('TotalClaimed1').value),
    fsabox = parseInt(this.getField("FSA_Total").value);

    if( this.getField('FSA1').value == 'Yes')
    fsabox += claim1;
    if( this.getField('FSA2').value == 'Yes')
    fsabox += claim2;

    event.value = fsabox;​

    ----------------

    Something is causing it to add every time the box is checked. Like the checking off of the box is what causes the addition.

    Its almost reading the code as "When the box is checked, add the value to the total", so each time the box gets checked, it adds to the total again.
    and it should be "IF the box is checked, add the value to the total" so if there is a value in the field, and the box is checked, that value is passed on to the total; and if the box is not checked, the value is ignored.

    Does this make sense?

  12. #12
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I can't really help you when you only keep showing me part of the code, just show me the entire function.

    This entire time you still haven't told me what event is...

    I'm guessing this is inside the Checkbox.onchange() event, but I don't know.

  13. #13
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think JavaScript might function a bit differently in HTML than in a PDF document.
    In the PDF, I am selecting the FSA_Total field. In the properties box, there is a "calculate using javascript" button.
    That gives me a little window to put the JavaScript. The only thing in that window is:

    var claim1 = parseInt(this.getField('TotalClaimed1').value),
    claim2 = parseInt(this.getField('TotalClaimed1').value),
    fsabox = parseInt(this.getField("FSA_Total").value);

    if (this.getField('FSA1').value == 'Yes') {
    fsabox += claim1;
    }

    if(this.getField('FSA2').value == 'Yes') {
    fsabox += claim2;
    }

    event.value = fsabox;​

    -----------------------------

    Thank you VERY much for your patience. I appreciate all the help you've given me already.

  14. #14
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OH! That's because you ARE adding the value to the total in this line here:
    fsabox = parseInt(this.getField("FSA_Total").value);

    I can't believe I didn't see that. Do this:
    fsabox = 0;

    You were basically saying: get whatever is in FSA_Total right now and start adding to it. You want to start from 0 each time, and add to that.

  15. #15
    SitePoint Member
    Join Date
    May 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOOHOO!!!!! THANKS!

    That did it!

    HOORAY for OMGCARLOS!!!!

    SUPERTHANKS!!!!111ElebentyOne!!!!11!!!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •