Link to home
Start Free TrialLog in
Avatar of MariusGM
MariusGM

asked on

Simple form calculation within repeated region

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

ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
Forced accept.

Computer101
Community Support Moderator