Forum Moderators: open

Message Too Old, No Replies

Javascript Calculator Help

Javascript Calculator Help

         

Raven_R1

12:02 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Hello im new to this site, and was in need of some help with a calculator i compiled using Javascript. this is for a refference calculator only. on the web page there is data table that shows price of an item. and is totalled up and shows a sub total value.

using an example of a cookie shoping cart i found online it creates the table as you add items to the cookie,

here is the what i am using:

// Get from cookie
function GetFromCart() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("¦", 0);
Token1 = database.indexOf("¦", Token0+1);
Token2 = database.indexOf("¦", Token1+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 );
fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

whattowrite += "<tr><td>" + fields[2] + "</td><td><font size=-1>"
+ fields[3] + "</font></td><td>$" + fields[1]
+ "<td><input type=button value=\" Remove \" onClick=\"RemoveFromCart("+i+")\"></td>"
+ "<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">"
+ "<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">"
+ "<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">";
}

document.write(whattowrite);
document.write("</td></tr><tr><td colspan=2><b>SUBTOTAL</b></td><td>$");
document.write(TOTotal);<<<<<<<<<<<<<<<<<<<< This value is what i want to use in the form calculaor
document.write("</td><td></td>");
}

¦
// WriteToForm

function WriteToForm() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("¦", 0);
Token1 = database.indexOf("¦", Token0+1);
Token2 = database.indexOf("¦", Token1+1);

fields = new Array;

fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

document.write("<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">");
document.write("<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">");
document.write("<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">");

}
}

now the problem i want to solve is when i use the calculator:

function showpay() {
if ((document.calc2.loan2.value == null ¦¦ document.calc2.loan2.value.length == 0) ¦¦
(document.calc.months.value == null ¦¦ document.calc.months.value.length == 0)
¦¦
(document.calc.rate.value == null ¦¦ document.calc.rate.value.length == 0))
{ document.calc.pay.value = "Incomplete data";
}
else
{
var princ = document.calc2.total.value
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
document.calc.pay.value = Math.round (princ * intr / (1 - (Math.pow(1/(1 + intr), term)))*100)/100;
}
}

I want the "document.calc2.total.value" to grab the information that was written in the table "document.write(TOTotal);"

In the calculator i have set the document.calc2.total.value to equal the document.write(TOTotal);
but that is not working, how can i go about solving my problem? if you can help me out i would really appreciate it thanks

kevinjohn

5:42 am on Jun 13, 2005 (gmt 0)

10+ Year Member



Raven,

I believe that you can solev this issue by declaring the "TOTotal" variable as a global variable in the javascript.As per the code you have provided here,"TOTotal" is a local variable inside the "GetFromCart() " function. So,you cannot access the value of "TOTotal" inside the "showpay()" function.

Solution:

1) Declare the "TOTotal" variable as a global variable in the javascript so that its value can be accessed by any javascript function in that page.

Hope this one helps you.
Good Luck.