Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to calculate form value

Status
Not open for further replies.

necta

Technical User
Mar 8, 2004
35
MY
I have a dynamic generated form fields which allow only number. I intent to calculate the total and average before submitting it to the database. The total is get by multiple the marks value with a credit and put it in totalMarks. The average is calculate after that and put the value on the aveMarks field. The script run no error but return no result in totalMarks and aveMarks fields. Can anyone please kindly help me. Thank you

Necta

<form action=abc.asp method=post name=form1>
<%
subjNo=0
Do While NOT sRS.EOF
subjNo=subjNo+1

%>
<td width="150"><%=sRS("subjName")%></td>
<td colspan="4">

<input type="text" name="marks<%=subjNo%>" id="marks<%=subjNo%>" size="4" maxlength="3">

<input type="hidden" name="subjID<%=subjNo%>" value="<%= sRS("subjID")%>">

<input type="hidden" name="credit<%=subjNo%>" value="<%=cRS("subjCrd")%>">
</tr>
<%
sRS.moveNext
Loop
<input type="text" maxlength="3" name="totalMarks" size="6" <%="READONLY"%>>
<input type="button" onclick=calc(form1) name="calculate" value="Hitung">

<td colspan="4"><input type="text" maxlength="3" name="aveMarks" size="6" <%="READONLY"%>>
</form>

<script language="javascript">
<!--

function calc(frm)
{
for (i=0; i < frm.subjNo; i++)
{
if (frm.marks.value != -1)
var totalMarks=totalMarks + (marks*credit);
var totalCredit=totalCredit + credit;
var aveMarks=totalMarks/totalCredit;
formarks.totalMarks.value=totalMarks;
formarks.aveMarks.value=aveMarks;

return;

}
}
//-->
</script>
 
You should declare the totalMarks, totalCredit and aveMarks variables outside the loop. They are getting reset each time.

Regards,

Patrick
 
Hi Patrik,
i have change the var place but still get the same result.but the result still the same. no totalMarks or aveMarks value display.

rgrds.
Necta
 
as shown below:
function calc(frm)
{var totalMarks;
var totalCredit;
var aveMarks;
for (i=0; i < frm.subjNo; i++)
{
if (frm.marks.value != -1)
totalMarks=totalMarks + (marks*credit);
totalCredit=totalCredit + credit;
aveMarks=totalMarks/totalCredit;
frm.totalMarks.value=totalMarks;
frm.aveMarks.value=aveMarks;

return;

}
}
//-->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top