236 users online (0 members and 236 guests)

# Thread: Form Does Calculations Problem

Results 1 to 10 of 10
1. ## 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,  Reply With Quote

2. ## 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.   Reply With Quote

3. ## 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>```  Reply With Quote

4. ## 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.  Reply With Quote

5. ## Re: Form Does Calculations Problem

Hi there Lorib01,

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

6. ## 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;
df.focus();
df.onclick=function() {
doSums();
}
df.onclick=function() {
df.focus();
}
}
function doSums(){
hours=parseFloat(df.value);
if(isNaN(hours)) {
alert('please enter hours');
df.value='';
df.focus();
return;
}
else {
df.focus();
}
fld1=parseFloat(df.value);
if(isNaN(fld1)) {
alert('please enter field 1 cost');
df.value='';
df.focus();
return;
}
else {
df.focus();
}
fld2=parseFloat(df.value);
if(isNaN(fld2)) {
alert('please enter field 2 cost');
df.value='';
df.focus();
return;
}
df.value='£ ' +(fld1+fld2).toFixed(2);
df.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>```  Reply With Quote

7. ## 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  Reply With Quote

8. ## 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.   Reply With Quote

9. ## 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.  Reply With Quote

10. ## Re: Form Does Calculations Problem

Hi there Lorib01,

document.forms refers to the first form on the page.

document.forms refers to the first element within that form.

document.forms refers to the second element within that form.

document.forms refers to the second option of a select element within that form, assuming, of course, that document.forms is a select element. I hope that this helps.   Reply With Quote

#### Tags for this Thread

calculations, form, problem 