PDA

View Full Version : Javascript and php code for autocalculation



rhodarose
10-17-2011, 03:54 AM
Hi!


I used javascript for my autocalculate and it works and now my problem is how can I SUM the total from autocalculate and the Data from other textbox.

here is my code:



function autocalearn(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotEarn += val; //accumulate
}
}
oForm.TotEarn.value = TotEarn.toFixed(2); //out
}

</script>


The scenario is I have a Amount textbox and it has a value from database. And I have textboxes which is autocalculate and the total of that is display in total earn and now i want that the total earn and total amount will SUm and display in total earn..



<input name= "Amount" class = "LFieldTot" type="text" maxlength="12" tabindex="0" size="12" value="{$Amount}" readonly="readonly"/>


Thank you...

I tried to put {Amount} in Total Earn Values but when I insert data in the earnings textbox the data was the calculate from the textboxes, it did not sum the Amount.

rhodarose
10-17-2011, 05:40 AM
Good day!

I tried to edit the codes and i want to happen is the value of textbox amount will add also in the autocalculated.

here is the code:


<script type="text/javascript" language="javascript">

function autocalearn(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotEarn += val; //accumulate
}
}
//oForm.TotEarn.value = TotEarn.toFixed(2); //out
oForm.TotEarn.value = TotEarn.toFixed(2) + document.getElementById('Amount').value;
}

</script>


the output of this code is when I input 10 the amount data is 100 the total earn is 10100..its wrong..I want result is 110.