[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Simple form calculation within repeated region

Posted on 2007-11-29
Medium Priority
691 Views
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>
<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>
<body>
<form name="myform">
<input type="text" name="txt1" id="txt1" onkeyup="calc(this.value);"/>
</form>
</body>
</html>
``````
0
Question by:MariusGM
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 24

Accepted Solution

glcummins earned 1000 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;
}
``````
0

LVL 75

Assisted Solution

Michel Plungjan earned 1000 total points
ID: 20375128
try this:

``````<html>
<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>
<body>
<form name="myform">
<input type="text" name="txt1" id="txt1" onkeyup="calc(this);">
<input type="text" name="txt2" id="txt2" onkeyup="calc(this);">
</form>
</body>
</html>
``````
0

LVL 75

Expert Comment

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

ID: 20943733
Forced accept.

Computer101
Community Support Moderator
0

## Featured Post

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creatâ€¦
###### Suggested Courses
Course of the Month13 days, 6 hours left to enroll