Solved

Simple form calculation within repeated region

Posted on 2007-11-29
5
686 Views
Last Modified: 2008-02-20
Hi, I need to perform a simple multiplication calculation within a form that has repeated rows within it.

I have a basic Javascript script that will multiply, by a fixed value, the value a user enters into a textbox and displays it within a readonly textbox beside it. See code below.

I need to be able to use this script (or something similar) in a repeated table within PHP.
<html>
<head>
<script language="javascript" src="remote.js"></script>
<script type="text/javascript">
    function calc(num)
    {
          var multiplier = 2; //your number here
          var result = 0;
          if(isFinite(num))
          {
                result = num * multiplier;
          }
          result = result.toFixed(0);
          document.myform.result.value = result;
    }
</script>
</head>
<body>
<form name="myform">
<input type="text" name="txt1" id="txt1" onkeyup="calc(this.value);"/>
<input type="text" name="result" id="result" readonly="readonly"/>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:MariusGM
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 20375117
Add another parameter to your function that identifies the output field (code below). Then give each of your ouput fields an id, and supply them when you call the function.


function calc(num, fieldID)
{
      var multiplier = 2; //your number here
      var result = 0;
      if(isFinite(num))
      {
            result = num * multiplier;
      }
      result = result.toFixed(0);
      document.getElementById(fieldId).value = result;
}

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 20375128
try this:


<html>
<head>
<script language="javascript" src="remote.js"></script>
<script type="text/javascript">
function calc(theField) {
  var num = theField.value;
  var theForm =theField.form;
  var res = theField.name.replace('txt','res');
  var multiplier = 2; //your number here
  var result = 0;
  if(isFinite(num)) {
    result = num * multiplier;
  }
  result = result.toFixed(0);
  theForm.elements[res].value = result;
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="txt1" id="txt1" onkeyup="calc(this);">
<input type="text" name="res1" id="res1" readonly="readonly"/>
<input type="text" name="txt2" id="txt2" onkeyup="calc(this);">
<input type="text" name="res2" id="res2" readonly="readonly"/>
</form>
</body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20375149
PS:

 var result = 0;
  if(isFinite(num)) {
    result = num * multiplier;
  }
  result = result.toFixed(0);
  theForm.elements[res].value = result;

can be shortened to

  theForm.elements[res].value = (isFinite(num))? (num * multiplier).toFixed(0):0;
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20943733
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

770 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