?
Solved

How do I get the SUM of 'a' textfields in javascript before I submit the form?

Posted on 2008-11-12
5
Medium Priority
?
533 Views
Last Modified: 2012-08-14
Hi All!
I already have the solution (provided by und3ath) to this question but this query is related to the solution. Please do refer to the solution (ID:23665873). To get the no. of rows of html elements I had iterated '10' times. What I need to do now is change that value from '10' to a variable say 'a'. This variable gets its value from a textfield (name="a"). The iteration works fine but the Javascript function getsum() does not work. why is that? please can anyone help me?
<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum">
          </table>
          <script type="text/javascript">
function getsum(){var sum=0;
for(i=1;i<=10;i++)
    sum+=Number(document.getElementById('total_wt'+i).value);
document.getElementById('sum').value=sum;}
 
setInterval("getsum()",1);
</script>

Open in new window

0
Comment
Question by:786aslamkhan
5 Comments
 
LVL 9

Expert Comment

by:darkapple
ID: 22947696
don't refrence text elements in form by document.getElementById...

try

document.formName.elementName.value it should work fine i guess.

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22947731
try:
<form name="myForm">
<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum" value="">
          </table>
</form>
          <script type="text/javascript">
function getsum(){
	var f=document.forms.myForm;
	var sum=0;
	for(var i=0, limit=f.elements.length; i < limit; i++)
	{
		if( f.elements[i].name.indexOf("total_wt")==0 )
    			sum+=Number(f.elements[i].value);
	}
	document.getElementById('sum').value=sum;
}
 
setInterval("getsum()",1);
</script>

Open in new window

0
 
LVL 15

Expert Comment

by:quincydude
ID: 22947760

<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum">
          </table>
          <script type="text/javascript">
function getsum(){var sum=0;
for(i=1;i<=10;i++)
{
	if(document.getElementById('total_wt'+i) != null)
		sum+=Number(document.getElementById('total_wt'+i).value);
}
document.getElementById('sum').value=sum;}
 
setInterval("getsum()",1);
</script>

Open in new window

0
 

Author Closing Comment

by:786aslamkhan
ID: 31516276
Thanks a ton hielo! The soultion is absolutely perfect. Thanks for your time, attention and help. You are a true GENIUS!!! I have learnt this from you!
0
 

Author Comment

by:786aslamkhan
ID: 22947809
Thanks quincydude for your solution- I had already tried and accepted  hielo's solution (and Awarded the points) which is perfect. I have not yet tried yours but I will later just for learning. However please do accept my thanks and gratitude for your precious time, attention and offered solution.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question