36 users online (1 members and 35 guests)  


  Results 1 to 10 of 10

Related

  1. calculations using Javascript    Forum: Javascript Forum
    Replies: 2
  2. Funny Problem With Form    Forum: PHP Forum
    Replies: 1
  3. Help required with number calculations    Forum: CGI Perl Forum
    Replies: 7
  4. Replies: 1
  1. #1
    Lorib01's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2007
    Posts
    19

    Form Does Calculations Problem

    Hello,

    I have created a form that user will input the hours worked and the amount of tasks completed in two fields. I need to make the form add these two fields together, display the total and then divide the total by the total hours worked.
    I believe this is possible with JavaScript but I'm a bit of a newbie. Can anyone help me with this script?

    Thank you,

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Form Does Calculations Problem

    Hi there Lorib01,

    your explanation seems rather vague.
    Could you supply an example, using values, to show how the calculator is meant to work?
    Once we have that, then the javascripting required will be forthcoming.

  3. #3
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: Form Does Calculations Problem

    Here is a simplified example of what I think you're looking for, although it doesn't grab fields from a form. You should be able to figure out that part.

    Code:
    <script type="text/javascript">
    var hours_worked = 15;
    var number_completed_tasks = 4;
    var hours_per_task = hours_worked / number_completed_tasks;
    document.write (hours_per_task);
    </script>
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more

  4. #4
    Lorib01's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2007
    Posts
    19

    Re: Form Does Calculations Problem

    I'm sorry I was vague. I've attached a sample of what the code looks like for this form. The user should fill in the hours worked with will be a variable, and they will fill in field 1 & 2. The form should add field 1 & 2 and put it in the total field and then it should divide the total by the number of ours worked.

    Code:
    <html>
    <head>
    </head>
    <body>
    <form name=qdbform method=POST onsubmit='return validateForm(this)' encType='multipart/form-data' action=https://www.quickbase.com/db/bcuh33rwr?act=API_AddRecord>
    <input type=hidden name=fform value=1>
    <table>
    <tr><td class=m><font color=red size=+1>* </font>Total Hours Worked</td>
    <td class=m><input type=text size=10 name=_fid_24 ></td></tr>
    <tr><td class=m><font color=red size=+1>* </font>Field 1</td>
    <td class=m><input type=text size=10 name=_fid_25 ></td></tr>
    <tr><td class=m><font color=red size=+1>* </font>Field 2</td>
    <td class=m><input type=text size=10 name=_fid_26 ></td></tr>
    <tr><td class=m><font color=red size=+1>* </font>Total of Field 1 and 2</td>
    <td class=m><input type=text size=10 name=_fid_27 ></td></tr>
    <tr><td class=m><font color=red size=+1>* </font>Total of Fields divided by Work Hours</td>
    <td class=m><input type=text size=10 name=_fid_28 ></td></tr>
    </table>
    </body>
    </html>
    Thanks for your help.

  5. #5
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Form Does Calculations Problem

    Hi there Lorib01,

    are fields 1 &2 monetary values -(dollars or sterling) - or just numbers?

  6. #6
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Form Does Calculations Problem

    Hi there Lorib01,

    of course, it does not really matter if field 1 and field 2 are monetary or not, the calculation is the same.
    So I have assumed that it is monetary and coded for pounds sterling.
    It is quite easy to change it to whatever you require...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    .m span {
        color:#f00;
        font-size:18px;
    }
    </style>
    
    <script type="text/javascript">
       var df;
    window.onload=function(){
       df=document.forms[0];
       df[1].focus();
    df[6].onclick=function() {
       doSums();
      }
    df[7].onclick=function() {
       df[1].focus();
      }
     }
    function doSums(){
    hours=parseFloat(df[1].value);
    if(isNaN(hours)) {
       alert('please enter hours');
       df[1].value='';
       df[1].focus();
       return;
     }
    else {
       df[2].focus();
     }
    fld1=parseFloat(df[2].value);
    if(isNaN(fld1)) {
       alert('please enter field 1 cost');
       df[2].value='';
       df[2].focus();
       return;
     }
    else {
       df[3].focus();
     }
    fld2=parseFloat(df[3].value);
    if(isNaN(fld2)) {
       alert('please enter field 2 cost');
       df[3].value='';
       df[3].focus();
       return;
     }
       df[4].value=' ' +(fld1+fld2).toFixed(2);
       df[5].value=' '+((fld1+fld2)/hours).toFixed(2);
    }
    </script>
    
    </head>
    <body>
    
    <form name="qdbform" method="post" encType="multipart/form-data" action="https://www.quickbase.com/db/bcuh33rwr?act=API_AddRecord" onsubmit="return validateForm(this)">
    
    <table><tr>
    <td class="m"><span>* </span>Total Hours Worked</td>
    <td class="m"><input type="hidden" name="fform" value="1"/><input type="text" size="10" name="_fid_24" /></td>
    </tr><tr>
    <td class="m"><span>* </span>Field 1</td>
    <td class="m"><input type="text" size="10" name="_fid_25" ></td>
    </tr><tr>
    <td class="m"><span>* </span>Field 2</td>
    <td class="m"><input type="text" size="10" name="_fid_26" /></td>
    </tr><tr>
    <td class="m"><span>* </span>Total of Field 1 and 2</td>
    <td class="m"><input type="text" size="10" name="_fid_27" readonly="readonly" /></td>
    </tr><tr>
    <td class="m"><span>* </span>Total of Fields divided by Work Hours</td>
    <td class="m"><input type="text" size="10" name="_fid_28" readonly="readonly"/></td>
    </tr><tr>
    <td class="m"><input type="button" value="calculate"/></td>
    <td class="m"><input type="reset" value="clear fields"/ ></td>
    </tr></table>
    
    </form>
    
    </body>
    </html>
    Last edited by coothead; 09-17-2007 at 11:19 AM.

  7. #7
    Lorib01's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2007
    Posts
    19

    Re: Form Does Calculations Problem

    It is not money but tasks completed. I appreciate your work an think that I should be able to make a few changes in the code so it doesn't calculate money.

    Thank you very much

  8. #8
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Form Does Calculations Problem

    Hi there Lorib01,

    OK, just remove the symbols that I highlighted in red and also the plus signs that follow.

  9. #9
    Lorib01's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2007
    Posts
    19

    Re: Form Does Calculations Problem

    Thank you, I was able to remove the symbols, that was no problem.

    Can someone explain how this works so I can understand it better? I don't understand how the form knows which fields are which (like how does it know that fld1 is Field 1).

    I'd appreciate it, thanks.

  10. #10
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Form Does Calculations Problem

    Hi there Lorib01,

    document.forms[0] refers to the first form on the page.

    document.forms[0][0] refers to the first element within that form.

    document.forms[0][1] refers to the second element within that form.

    document.forms[0][2][1] refers to the second option of a select element within that form, assuming, of course, that document.forms[0][2] is a select element.

    I hope that this helps.



Tags for this Thread