Solved

Simple form calculation within repeated region

Posted on 2007-11-29
5
684 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now