473,466 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculations in form with missing vars

Trying to total some price fields in a form but doesnt work when all the
referenced form fields dont exisit.
This is for an invoice - pulled prom a database and the form doesnt always
contain the same amount of Line Items.
If I have all 20 Line Items, it works great.

var sub1 = form.Line_Item_Subtotal1.value
var sub2 = form.Line_Item_Subtotal2.value
var sub3 = form.Line_Item_Subtotal3.value
var sub4 = form.Line_Item_Subtotal4.value
var sub5 = form.Line_Item_Subtotal........>

var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)

How can this be written to work with any number of Line_Item_Subtotal(n)
fields?

Thanks!

Jul 23 '05 #1
4 2091
Targa wrote:
var sub1 = form.Line_Item_Subtotal1.value
var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)
How can this be written to work with any number of
Line_Item_Subtotal(n) fields?


something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 23 '05 #2
Thanks for the reply!
Now Im getting 'document.form.calculations' is null or not an object.
Any ideas?

Here is the entire script:
<SCRIPT LANGUAGE="JavaScript"><!--
// INVOICE CALCULATIONS
function calc(form) {
var sum = 0;
var rowsum;
var quantity = 1
// Add Lines (Line_Item_Subtotalx)

if ( parseFloat(form.Line_Qty1.value) &&
parseFloat(form.Line_Unit_Price1.value) ) {
quantity += parseInt(form.Line_Qty1.value);
form.Line_Qty1.value = parseInt(form.Line_Qty1.value);
form.Line_Unit_Price1.value = parseFloat(form.Line_Unit_Price1.value);
rowsum = form.Line_Qty1.value * form.Line_Unit_Price1.value;
sum += rowsum;
form.Line_Unit_Price1.value = money(form.Line_Unit_Price1.value);
form.Line_Item_Subtotal1.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty2.value) &&
parseFloat(form.Line_Unit_Price2.value) ) {
quantity += parseInt(form.Line_Qty2.value);
form.Line_Qty2.value = parseInt(form.Line_Qty2.value);
form.Line_Unit_Price2.value = parseFloat(form.Line_Unit_Price2.value);
rowsum = form.Line_Qty2.value * form.Line_Unit_Price2.value;
sum += rowsum;
form.Line_Unit_Price2.value = money(form.Line_Unit_Price2.value);
form.Line_Item_Subtotal2.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty3.value) &&
parseFloat(form.Line_Unit_Price3.value) ) {
quantity += parseInt(form.Line_Qty3.value);
form.Line_Qty3.value = parseInt(form.Line_Qty3.value);
form.Line_Unit_Price3.value = parseFloat(form.Line_Unit_Price3.value);
rowsum = form.Line_Qty3.value * form.Line_Unit_Price3.value;
sum += rowsum;
form.Line_Unit_Price3.value = money(form.Line_Unit_Price3.value);
form.Line_Item_Subtotal3.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty4.value) &&
parseFloat(form.Line_Unit_Price4.value) ) {
quantity += parseInt(form.Line_Qty4.value);
form.Line_Qty4.value = parseInt(form.Line_Qty4.value);
form.Line_Unit_Price4.value = parseFloat(form.Line_Unit_Price4.value);
rowsum = form.Line_Qty4.value * form.Line_Unit_Price4.value;
sum += rowsum;
form.Line_Unit_Price4.value = money(form.Line_Unit_Price4.value);
form.Line_Item_Subtotal4.value = money(rowsum)
}
//GET SUBTOTAL
var total=0;
var theform = document.form["calculations"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);

}
function money(num) // converts from floating point to money format
{
var amount = Math.abs(num);
var pounds = Math.floor(amount);
var pence = Math.round( 100*(amount-pounds) );
if(pence>99) pence=0, pounds++;
pence += ""
while (pence.length < 2) pence = "0" + pence;
amount = pounds + "." + pence;
if (num < 0) return "[" + amount + "]";
return amount;
}

//-->
</SCRIPT>


"Matt Kruse" <ne********@mattkruse.com> wrote in message
news:c9*********@news1.newsguy.com...
Targa wrote:
var sub1 = form.Line_Item_Subtotal1.value
var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)
How can this be written to work with any number of
Line_Item_Subtotal(n) fields?


something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/


Jul 23 '05 #3
"Targa" wrote:
Thanks for the reply!
Now Im getting 'document.form.calculations' is null or not an object.
Any ideas?

<snip>

It is - document.forms - (forms is plural).

Richard.
Jul 23 '05 #4
JRS: In article <Pq*************@fe39.usenetserver.com>, seen in
news:comp.lang.javascript, Targa <ta***********************@alltel.net>
posted at Wed, 2 Jun 2004 12:47:34 :
quantity += parseInt(form.Line_Qty1.value);
Can you be sure that .value will never be entered as, for example, '08'
or '09'? See FAQ.

function money(num) // converts from floating point to money format
{
var amount = Math.abs(num);
var pounds = Math.floor(amount);
var pence = Math.round( 100*(amount-pounds) );
if(pence>99) pence=0, pounds++;
pence += ""
while (pence.length < 2) pence = "0" + pence;
amount = pounds + "." + pence;
if (num < 0) return "[" + amount + "]";
return amount;
}
Note - numbers entered as #.##5 round in a different manner in different
methods. ISTM that your "while" could be an "if", and
amount = pounds + "." + LZ(pence)
could replace 3 lines above, if LZ is known. The method gives an
improper format for vast numbers (GBP 10^21 and up).

We should consider whether the result from, say, -0.002 should show as
negative zero. Likewise for +0.002 in a method where + signs are shown.

"Matt Kruse" <ne********@mattkruse.com> wrote in message


Responses should go after trimmed quotes; see FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Xenophobe | last post by:
I would like to use the same form for adding new records and editing existing records. New form values are contained in $_POST. Database values are contained in an associated array. It seems to me...
4
by: Stephanie Stowe | last post by:
You have a page. On top of the page, there are some search criteria and other input elements and a submit button. The submit button is clicked, sending a request to the server. Server-side code is...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
4
by: Ben McLaurin | last post by:
When oopeening a form based of a table I need to run a function to do some calculations. Which form event do I need to use that will automatcally run when the form is opened but all the data from...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
1
by: CBFalconer | last post by:
I have, for my own amusement, written the following code. However I am in grave doubts as to the accuracy of the code in the function "variations", having spent 50 odd years forgetting everything...
61
by: bonneylake | last post by:
Hey Everyone, Well after asking many questions i have this almost working. This is how it works. Basically i fill in my customer number field an that populates my drop down box. Once i select...
9
by: stack | last post by:
Hi all, I am creating a web app using JSP. I have a bean with some variables whose values are set by the user by using a form in a jsp (method is post). When the user enters those values these are...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.