SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding form fields on the fly

    Hi all - I'm attempting to sum/add up values within 4 different form fields on the fly, but keep getting a result of NaN....not sure what I'm doing wrong....here's my code:


    function calculate(totalElement) {

    for (var i=1,app_pend_end=0;i<5;i++)
    app_pend_end += what.elements['textField' + i].value - 0;
    totalElement.app_pend_end.value = app_pend_end;

    if (totalElement)
    {
    var calculation = "";
    var overall = "";
    var fields = new Array();
    var userInputs = myform.elements;

    for (var f = 0; f < userInputs.length; f++)
    {
    if (userInputs[f].className=='special_value')
    {
    if (userInputs[f].value)
    {
    fields[f] = userInputs[f].value;
    }
    else
    {
    fields[f] = 0;
    }
    }
    }

    for (var i=0; i<fields.length; i++)
    {
    calculation += fields[i];

    if (i!=fields.length-1)
    {
    calculation += '+';
    }
    }

    document.writeln(calculation);

    if (calculation!='')
    {
    overall = eval(calculation).toFixed(2);
    }

    if (overall!='')
    {
    totalElement.app_pend_end.value = overall;
    }
    }
    }
    Last edited by isskohr; Sep 22, 2010 at 10:43. Reason: need to clarify

  2. #2
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgot to mention, this code is within a PHP file....I'm adding up the values, then submitting the info into a database

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all - think I found my answer.....using different code that works in IE, although it doesn't quite work in Friefox, which I believe is b/c of the onKeyPress event

    function calculate()
    {

    AppPend = 0; AppRec = 0; AppAppr = 0; AppClosed = 0;
    TotAmt = 0;

    if (document.myform.app_pend.value > "")
    { AppPend = document.myform.app_pend.value };
    document.myform.app_pend.value = eval(AppPend);

    if (document.myform.app_rec.value > "")
    { AppRec = document.myform.app_rec.value };
    document.myform.app_rec.value = eval(AppRec);

    if (document.myform.app_appr.value > "")
    { AppAppr = document.myform.app_appr.value };
    document.myform.app_appr.value = eval(AppAppr);

    if (document.myform.app_closed.value > "")
    { AppClosed = document.myform.app_closed.value };
    document.myform.app_closed.value = eval(AppClosed);


    TotAmt =
    eval(AppPend) +
    eval(AppRec) -
    eval(AppAppr) -
    eval(AppClosed) ;

    document.myform.app_pend_end.value = eval(TotAmt);

    }

  4. #4
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just discovered I had an error in my form....works great now!


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
  •