...

View Full Version : Grand Total Javascript Math



sftl99
07-05-2005, 10:07 PM
Here is my current page (http://www.jasonpcarroll.com/allcomponents/quote.html) . "View Source" is going to work a lot better than me pasting the code here because there is a ton of javascript in this page. I have added a Grand Total box at the bottom right of the page.

function grandtotal() {
document.quoteform.GrandTotal.value = "$" + round(document.quoteform.Extension.value + document.quoteform.Extension2.value + document.quoteform.Extension3.value + document.quoteform.Extension4.value + document.quoteform.Extension5.value)}
The above code is running when ' onBlur="grandtotal()" ' after each "Extension" field. My problem is that after the "$" has been added to the prior fields, the javascript math function won't work. I can obviously rewrite the code and make a GrandTotal() for every time you add a new set of Qty and Price, but my script is already inefficient as is, so I'm trying to think of a workaround.

Maybe I should start from scratch...but is there a way to do what I'm doing is a much more effecient way? ...ok, I'm sure most of my scripting is lame, but it works, so I'm specifically referring to the math functions. any ideas are appreciated.

ca_redwards
07-05-2005, 10:57 PM
Here is some code I wrote for this purpose:



String.prototype.reverse=function(){return this.split('').reverse().join('')};
function number(d){ return d.match(/(\-|\d|\.)/gi).join(''); };
function dollar(n){
var s=(n+'.00').split('.');
s[0]=((s[0]+'').reverse().match(/\d{1,3}\-?/gi)+'').reverse(); //greedy RegExp gathers groups of 3 digits
s[1]=(s[1]+'00').slice(0,2);
return("$"+s[0]+'.'+s[1]);
};
function update(quantity,price,extension,total){
with(total)value=0;
for(var i=0;i<extension.length;i++){
with(quantity[i])value=number(value);
with(extension[i]){
value=quantity[i].value*number(price[i].value);
total.value-= -value;
value=dollar(value);
};
with(price[i])value=dollar(value);
};
with(total)value=dollar(value);
};


Then just rely upon like named fields acting as an array...


<input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
<input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
<input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
<input name="total" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />


Enjoy.

sftl99
07-05-2005, 11:50 PM
the formula works great, and i started with something similar in the sense that i wouldn't have to name each input differently in order to do the math, but my problem is that many times in my form, entire lines get left blank, and the blank fields do not get sent thru the email. my sendmail is a huge PHP with lots of formatting (type your own email into the "Cust. Email" field to send yourself an example) so appearance is everything. with the script you provided as-is, it fills in every field and thus causes zeros and "$" to appear in the sent email which is not what i desire.

that being said, is there any way to integrate the part of your script that adds the $ to what I currently have?

please let me know if my questions don't make any sense...i'm trying to explain as best i know how.

ca_redwards
07-06-2005, 12:00 AM
You could break out of the loop, if a blank is found.

In the update function...


if (quantity[i].value == '') break;
if (price[i].value == '') break;

sftl99
07-06-2005, 12:29 AM
ok, i'm moving to a test page now www.jasonpcarroll.com/allcomponents/test.html.

I can't quite figure out where to put the script you just gave me. When I simply plugged it in where you said, it didn't do anything. Any ideas? Or can you plug it in the full formula for me?

Ok, I just took out the default value of zero and it works the way i want it to without adding anything (duh, that was easy), however, it doesn't add the $ to the grand total until the very end once every field is filled in, but again, every field won't be filled in most of the time. Check out the sample page.

...edit #2...in I.E. it's actually telling me there's an error with this method of leaving out the default value.

ca_redwards
07-06-2005, 12:58 AM
Use this update function instead...


function update(quantity,price,extension,total){
with(total)value=0;
for(var i=0;i<extension.length;i++){
if(!(quantity[i].value=='' || price[i].value=='')){
with(quantity[i])value=number(value);
with(extension[i]){
value=quantity[i].value*number(price[i].value);
total.value-= -value;
value=dollar(value);
};
with(price[i])value=dollar(value);
}
};
with(total)value=dollar(value);
};

sftl99
07-06-2005, 01:01 AM
that's beautiful! thank you. now i just need to redo some code! i appreciate the help big time.

sftl99
07-06-2005, 01:19 AM
For anyone who looked at this last problem I had and worked even one second on it, I deeply apologize. My error was in Dreamweaver adding code that I didn't want. It's fixed now, so sorry!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum