This topic is locked

Adding two fields together on the add page

7/17/2009 12:04:53 AM
ASPRunnerPro General questions
M
Matthew author

Is there a way to add to fields together and then display them in another field? I have [Starting Cash], [Starting Coin] and while these numbers are being typed in, I want it to add the two fileds and then display them in [Total Starting Cash]. Is there a way?

J
Jane 7/17/2009

Hi,
use custom JavaScript code for this purpose.

Here is a sample:

<script language=javascript>

var x = document.forms.editform.value_FieldName1;

var y = document.forms.editform.value_FieldName2;

x.onchange=x.onkeyup=y.onchange=y.onkeyup=function()

{

document.forms.editform.value_FieldName3.value = document.forms.editform.value_FieldName1.value+document.forms.editform.value_Fie

ldName2.value;

}

</script>



You can add JavaScript code at the end of the page on theVisual Editor tab in HTML mode.

M
Matthew author 7/17/2009

See Below.

M
Matthew author 7/17/2009

For some reason, it doesn't actually do any math. When I type in a value in the first field, like 32, then 32 in the next, the result is 3232. All it is doing is placing the numbers right next to each other.

M
Matthew author 7/18/2009

It's easier just to do the math in the SQL query. I would still like to see if the Javascript would work though.

J
Jane 7/20/2009

Hi,
try to use toFixed() function:

x.onchange=x.onkeyup=y.onchange=y.onkeyup=function()

{

var FieldName1 = document.forms.editform.value_FieldName1.value;

var FieldName2 = document.forms.editform.value_FieldName2.value;

document.forms.editform.value_FieldName3.value = FieldName1.toFixed()+FieldName2.toFixed();

}


More info here:

http://www.w3schools.com/jsref/jsref_tofixed.asp

C
clig 8/4/2009

It's easier just to do the math in the SQL query. I would still like to see if the Javascript would work though.


here's some calcs I used in 3.2 jsfunctions.js
function UpdateLabourTotal()

{

var lt;

var lt1;

var lt2;

lt1 = document.editform.Rate.value document.editform.Hours.value;

lt2 = document.editform.SetRate.value
document.editform.SetHours.value;

lt = parseFloat(lt1) + parseFloat(lt2);

return lt;

}
function UpdatePartsTotal()

{

var pt;

var pt1;

pt1 = document.editform.PartPrice.value document.editform.Quantity.value;

pt = parseFloat(pt1) + parseFloat(document.editform.Price.value);

return pt;

}
function UpdateGSTTotals()

{

var gstt;

var gst1;

var gst2;

var gst3;

gst2 = UpdateLabourTotal();

gst3 = UpdatePartsTotal();

gst1 = parseFloat(gst2) + parseFloat(gst3);

gstt = gst1
document.editform.GST.value;

return gstt;

}
function UpdatePSTTotals()

{

var pstt;

var pst1;

var pst2;

var pst3;

pst2 = UpdateLabourTotal();

pst3 = UpdatePartsTotal();

pst1 = parseFloat(pst2) + parseFloat(pst3);

pstt = pst1 * document.editform.PST.value;

return pstt;

}
function getLabourPartsTotal()

{

document.editform.LabourTotal.value = UpdateLabourTotal();

document.editform.PartsTotal.value = UpdatePartsTotal();

document.editform.GSTTotal.value = UpdateGSTTotals();

document.editform.PSTTotal.value = UpdatePSTTotals();

var var1;

var var2;

var var3;

var var4;

var1 = UpdateLabourTotal();

var2 = UpdatePartsTotal();

var3 = UpdateGSTTotals();

var4 = UpdatePSTTotals();

document.editform.Total.value = parseFloat(var1) + parseFloat(var2) + parseFloat(var3) + parseFloat(var4);

}

D
dsk 10/14/2009

I struggled with this as well and performed the following modification that seemed to correct the problem.

Modify code as follows:
<script language=javascript>

var x = document.forms.editform.value_FieldName1;

var y = document.forms.editform.value_FieldName2;

x.onchange=x.onkeyup=y.onchange=y.onkeyup=function()

{

document.forms.editform.value_FieldName3.value = document.forms.editform.value_FieldName1.value1+document.forms.editform.value_Fie

ldName2.value
1;

}

</script>