473,699 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ 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.v alue = money(sub21)

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

Thanks!

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


something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subt otal"+i].value-0);
}
theform.subtota l.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="JavaS cript"><!--
// INVOICE CALCULATIONS
function calc(form) {
var sum = 0;
var rowsum;
var quantity = 1
// Add Lines (Line_Item_Subt otalx)

if ( parseFloat(form .Line_Qty1.valu e) &&
parseFloat(form .Line_Unit_Pric e1.value) ) {
quantity += parseInt(form.L ine_Qty1.value) ;
form.Line_Qty1. value = parseInt(form.L ine_Qty1.value) ;
form.Line_Unit_ Price1.value = parseFloat(form .Line_Unit_Pric e1.value);
rowsum = form.Line_Qty1. value * form.Line_Unit_ Price1.value;
sum += rowsum;
form.Line_Unit_ Price1.value = money(form.Line _Unit_Price1.va lue);
form.Line_Item_ Subtotal1.value = money(rowsum)
}
if ( parseFloat(form .Line_Qty2.valu e) &&
parseFloat(form .Line_Unit_Pric e2.value) ) {
quantity += parseInt(form.L ine_Qty2.value) ;
form.Line_Qty2. value = parseInt(form.L ine_Qty2.value) ;
form.Line_Unit_ Price2.value = parseFloat(form .Line_Unit_Pric e2.value);
rowsum = form.Line_Qty2. value * form.Line_Unit_ Price2.value;
sum += rowsum;
form.Line_Unit_ Price2.value = money(form.Line _Unit_Price2.va lue);
form.Line_Item_ Subtotal2.value = money(rowsum)
}
if ( parseFloat(form .Line_Qty3.valu e) &&
parseFloat(form .Line_Unit_Pric e3.value) ) {
quantity += parseInt(form.L ine_Qty3.value) ;
form.Line_Qty3. value = parseInt(form.L ine_Qty3.value) ;
form.Line_Unit_ Price3.value = parseFloat(form .Line_Unit_Pric e3.value);
rowsum = form.Line_Qty3. value * form.Line_Unit_ Price3.value;
sum += rowsum;
form.Line_Unit_ Price3.value = money(form.Line _Unit_Price3.va lue);
form.Line_Item_ Subtotal3.value = money(rowsum)
}
if ( parseFloat(form .Line_Qty4.valu e) &&
parseFloat(form .Line_Unit_Pric e4.value) ) {
quantity += parseInt(form.L ine_Qty4.value) ;
form.Line_Qty4. value = parseInt(form.L ine_Qty4.value) ;
form.Line_Unit_ Price4.value = parseFloat(form .Line_Unit_Pric e4.value);
rowsum = form.Line_Qty4. value * form.Line_Unit_ Price4.value;
sum += rowsum;
form.Line_Unit_ Price4.value = money(form.Line _Unit_Price4.va lue);
form.Line_Item_ Subtotal4.value = money(rowsum)
}
//GET SUBTOTAL
var total=0;
var theform = document.form["calculatio ns"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subt otal"+i].value-0);
}
theform.subtota l.value = money(total);

}
function money(num) // converts from floating point to money format
{
var amount = Math.abs(num);
var pounds = Math.floor(amou nt);
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********@mat tkruse.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.v alue = money(sub21)
How can this be written to work with any number of
Line_Item_Subto tal(n) fields?


something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subt otal"+i].value-0);
}
theform.subtota l.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.usenetse rver.com>, seen in
news:comp.lang. javascript, Targa <ta************ ***********@all tel.net>
posted at Wed, 2 Jun 2004 12:47:34 :
quantity += parseInt(form.L ine_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(amou nt);
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********@mat tkruse.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
2309
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 that in order to use the same form for both tasks, I need to assign the form field values to page variables. The flow might look something like this: if $_POST $variable1 = $_POST; $variable2 = $_POST;
4
1418
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 executed to write client-side vars. Based on these vars, divs are shown or hidden. Why not just write or not write the html stuff server-side? I cannot understand it. And it is UGLY. If other client-events were going to interact with the divs,...
11
4665
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 having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
4
1585
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 the record is available. Tried a few that seems to make sense but I believe I am missing something obvious.
3
2575
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 groupings. The calculation is: unemployment rate = number of unemployed/number of labour force
1
1504
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 about these calculations. Criticism and suggestions welcomed. Note that the solutions presented to variations are all ordered, but may have trailing zeroes. /* find integral solutions to x*x + y*y + z*z ... = r*r */ /* Public domain, by...
61
5327
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 an option from the drop down box it populates the rest of the form. The problem i am having is that if a customer number doesn't exist in the drop down box an i try to put a new customer number in the input say 538 an then click on another field such...
9
5280
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 stored in my bean and they are available to all my jsps since I'm creating a "session". So, after the submission of the form the values are still displayed in the form and this is exactly what I want. In my jsp I have an if statement which causes...
9
2621
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 the main floors of the main house: Part C: Gross Floor Area of the basement or cellar: Part D: Gross Floor Area of the attic:
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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 we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.