473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get sum of input fields

I have created a form in asp. I used for loop to create text fields, obviously each text fields will have same name. Now I want the sum of these input fields. The sum should be displayed in total imports ( by adding 2 imports ) and total duty forgone( by adding 2 duty forgone).
Attached Files
File Type: txt annual9.txt (27.5 KB, 427 views)
May 19 '10 #1
7 3035
acoder
16,027 Expert Mod 8TB
You have multiple IDs which you should correct. IDs should be unique.

Anyway, if the name is the same for the ones that you need the sum of, use document.getElementsByName() to get all elements with the same name. Then loop over the collection and add them to a variable using parseInt/parseFloat to convert them to a number. Finally, set the total field to this value:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("tot_import").value = total;
See how you manage with that and if you get stuck, post your code.
May 19 '10 #2
Thanks for the replay ,

I tried with that code.great its working. here is the code

Expand|Select|Wrap|Line Numbers
  1. function DoSum1() //for looped sum
  2. {
  3.   var TheSum = 0;
  4.   for (i=1; i<=5; i++) {
  5.     TheSum += parseFloat(document.getElementById("txtExp_Fy1" + i).value-0);
  6.   }
  7.   document.getElementById("txtExp_tot1").value = TheSum;
  8. }

i was able to get the result for vertical columns but now the problem is to get the sum of horizontal i.e row wise total
May 19 '10 #3
acoder
16,027 Expert Mod 8TB
You'd get it in a similar manner, but it depends on how you're naming/ID-ing your text box elements vertically.
May 19 '10 #4
Thanks for the replay again,

Dear sir,
Here is the code written in "ASP" , Please tell me "How i can find the sum of text fields horizontally.


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <TITLE>EXIM - Progress Reports - Annual Details (Insert/Update) - Software Technology Parks Of India</title>
  4. </HEAD>
  5. <Body bgcolor="#FFFFFF" onload="return grand_tot();">
  6. <Form Action="Annual9.asp" method=post Name=frm >
  7. <Table border=1 cellspacing="0" cellpadding="0" align="center" width="60%" bgcolor=#F6E3CE>
  8. <Tr Bgcolor=#DF7401>
  9.     <td height="22" Align="left" colspan=13>
  10.         <Font color="#111111" Face="Arial" Size="2">
  11.         <B>16(a).&nbsp;CG Import from the date of Inception up to 21.03.2010[all Amount Rs in Lakhs]</B>
  12.         </Font>
  13.     </td>
  14. </Tr>
  15. <tr Bgcolor=#DF7401> 
  16.           <td width="3%"   align="center" valign="middle" rowspan="2"><font size="2" face="Arial, Helvetica" class="content"><b>Sl.No</b></font></td>
  17.           <td width="20%"  align="center" rowspan="2"><b>Imports Through </b></td>    
  18.           <td width="20%"  align="center" colspan="2"><b>As on 31.03.2009(From the Inception)</b></td>
  19.           <td width="20%"  align="center" colspan="2"><b>During Financial Year 2009-10</b></td>
  20.           <td width="20%"  align="center" colspan="2"><b>Total</b></td>
  21.  
  22. </tr>    
  23. <tr Bgcolor=#DF7401> 
  24.  
  25.           <td width="10%"  align="center"><b>Import</b></td>    
  26.           <td width="10%"  align="center"><b>Duty Foregone</b></td>
  27.           <td width="10%"  align="center"><b>Import</b></td>    
  28.           <td width="10%"  align="center"><b>Duty Foregone</b></td>
  29.           <td width="10%"  align="center"><b>Import</b></td>    
  30.           <td width="10%"  align="center"><b>Duty Foregone</b></td>       
  31. </tr>
  32. <%
  33.  
  34. Dim i
  35.  
  36. for i=1 to 10
  37.  
  38. if i=1 Then 
  39. %>
  40. <tr > 
  41.           <td width="3%"  align="center">a</td>
  42.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Out right Purchase</b></font></td>
  43. <%
  44. elseIf i=2 then
  45. %>
  46. <tr > 
  47.           <td width="3%"  align="center">b</td>
  48.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Loan Basis</b></font></td>
  49. <%
  50. elseIf i=3 then
  51. %>
  52. <tr > 
  53.           <td width="3%"  align="center">c</td>
  54.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Free of Charge</b></font></td>
  55. <%
  56. elseIf i=4 then
  57. %>
  58. <tr > 
  59.           <td width="3%"  align="center">d</td>
  60.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>High Sea Sale</b></font></td>
  61. <%
  62. elseIf i=5 Then 
  63. %>
  64. <tr > 
  65.           <td width="3%"  align="center">e</td>
  66.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Second Hand CG</b></font></td>
  67. <%
  68. elseIf i=6 then
  69. %>
  70. <tr > 
  71.           <td width="3%"  align="center">f</td>
  72.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Bond-Bond Transfer</b></font></td>
  73.  
  74. <%
  75. elseIf i=7 then
  76. %>
  77. <tr > 
  78.           <td width="3%"  align="center">g</td>
  79.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Deemed Import</b></font></td>
  80.  
  81. <%
  82. elseIf i=8 then
  83. %>
  84. <tr > 
  85.           <td width="3%"  align="center">h</td>
  86.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Import through courier mode</b></font></td>
  87. <%
  88. elseIf i=9 Then 
  89. %>
  90. <tr > 
  91.           <td width="3%"  align="center">i</td>
  92.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Leasing of Capital Goods</b></font></td>
  93. <%
  94. elseIf i=10 Then 
  95. %>
  96. <tr > 
  97.           <td width="3%"  align="center">j</td>
  98.           <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Permanent shifting from other units</b></font></td>
  99. <%End If
  100. %>
  101. <td width=10%   align=center> <input type=text name=txtExp_Fy11<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy11"&i&"")%>"                       
  102.  Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
  103.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  104.  </td> 
  105.  <td width=10%   align=center> <input type=text name=txtExp_inc12<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc12"&i&"")%>"
  106.   Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
  107.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  108.  </td>  
  109.  <td width=10%   align=center> <input type=text name=txtExp_Fy13<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy13"&i&"")%>"                       
  110.  Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
  111.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  112.  </td> 
  113.  <td width=10%   align=center> <input type=text name=txtExp_inc14<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc14"&i&"")%>"
  114.   Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
  115.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  116.  </td>  
  117.  <td width=10%   align=center> <input type=text name=txtExp_Fy15<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy15"&i&"")%>"                       
  118.  Onfocus="window.status='Enter Manpower from the day of inception' ;return true" readonly="readonly" onkeypress="return validateint(event);" onblur="return DoSum33() ;"
  119.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  120.  </td> 
  121.  <td width=10%   align=center> <input type=text name=txtExp_inc16<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc16"&i&"")%>"
  122.   Onfocus="window.status='Enter Manpower from the day of inception' ;return true"  readonly="readonly"  onkeypress="return validateint(event);" onblur="return DoSum34() ;"
  123.  Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
  124.  </td>  
  125.  </tr>
  126. <%
  127. next
  128. %>
  129. <tr align="center"> 
  130.           <td width="3%"  align="center"><b>A</b></td>
  131.           <td width="20%"   align="center" valign="middle"><font size="02" face="Arial, Helvetica" class="content"><b>Grand Total(a to j)</b></font></td>
  132.           <td width="10%" ><input type="text" name="txt_grandtot_inc_import" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>    
  133.           <td width="10%" ><input type="text" name="txt_grandtot_inc_dutyforegone" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>
  134.           <td width="10%" ><input type="text" name="txt_grandtot_during_import" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>
  135.           <td width="10%" ><input type="text" name="txt_grandtot_during_dutyforegone" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>                
  136.           <td width="10%" ><input type="text" name="txt_grandtot_tot_import" size="8" onkeypress="return validateint(event);" readonly="readonly"  "></td>
  137.           <td width="10%" ><input type="text" name="txt_grandtot_tot_foregone" size="8" onkeypress="return validateint(event);" readonly="readonly" "></td>                  
  138. </tr> 
  139. </tr> 
  140. </Table>
  141. </Body>
  142. </html>    

please check the attachement , you will get the clear picture what i real want to calculate. waiting for your replay ..........
Attached Images
File Type: jpg exim.jpg (18.9 KB, 183 views)
May 20 '10 #5
acoder
16,027 Expert Mod 8TB
See how you've used the ID here:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("txtExp_Fy1" + i)
Give the IDs of the elements going across similar style IDs, e.g. "textExp_Fy1_1", "textExp_Fy1_2" and so on, so then you can use a for loop to loop over them as you've done with the previous code.
May 20 '10 #6
Thaks a lot. I tried with the fallowing code .Its working.

Expand|Select|Wrap|Line Numbers
  1. function loopsum6() //to get the sum in horizontal way i.e alternate values
  2. {
  3.     var count=1;
  4.     while(count<=2)
  5.     {    
  6.         for (i=1; i<=2; i++)
  7.         {
  8.             if(count==1 && i==1)
  9.             {
  10.                 var val1 = parseFloat(document.getElementById("txtExp_inc1289"+ i).value-0);
  11.                 var val2 = parseFloat(document.getElementById("txtExp_inc1489"+ i).value-0);
  12.                 var ansD = document.getElementById("txtExp_inc1689"+ i);
  13.                 ansD.value = val1 + val2;
  14.                 break;
  15.             }
  16.             if(count==2 && i==2)
  17.             {
  18.                 var val1 = parseFloat(document.getElementById("txtExp_inc1289"+ i).value-0);
  19.                 var val2 = parseFloat(document.getElementById("txtExp_inc1489"+ i).value-0);
  20.                 var ansD = document.getElementById("txtExp_inc1689"+ i);
  21.                 ansD.value = val1 + val2;
  22.                 break;
  23.             }            
  24.         }
  25.         count++;
  26.     }
  27. }
May 21 '10 #7
acoder
16,027 Expert Mod 8TB
That's not the most efficient piece of code - a lot of unnecessary bloat and redundancy.

PS. threads merged. Please keep all posts in one thread instead of starting a new thread each time!
May 21 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.