PDA

View Full Version : SOLVED Grand Total Function

sftl99
07-06-2005, 04:12 PM
Here are the two different ways I'm doing my calculations:
Quote Form 1 (http://www.jasonpcarroll.com/allcomponents/quote.html) :: Quote Form 2 (http://www.jasonpcarroll.com/allcomponents/quote2.html) .

In the 1st one, the GrandTotal function does not work, so I moved to the 2nd example. My problem now is that in #2, all of the fields (Qty, Price, Extension) are all named the same, therefore when I submit the form to the PHP script, the results end up blank because of the repetition. Is there any way to work around this?

Code #1:

function round(number) {
return Math.round(number*100) / 100;
}
function dosum() {
document.quoteform.Extension.value = "\$" + round(document.quoteform.Price.value * document.quoteform.Qty.value)
document.quoteform.Price.value = "\$" + document.quoteform.Price.value}
function dosum2() {
document.quoteform.Extension2.value = "\$" + round(document.quoteform.Price2.value * document.quoteform.Qty2.value)
document.quoteform.Price2.value = "\$" + document.quoteform.Price2.value}
function dosum3() {
document.quoteform.Extension3.value = "\$" + round(document.quoteform.Price3.value * document.quoteform.Qty3.value)
document.quoteform.Price3.value = "\$" + document.quoteform.Price3.value}
function dosum4() {
document.quoteform.Extension4.value = "\$" + round(document.quoteform.Price4.value * document.quoteform.Qty4.value)
document.quoteform.Price4.value = "\$" + document.quoteform.Price4.value}
function dosum5() {
document.quoteform.Extension5.value = "\$" + round(document.quoteform.Price5.value * document.quoteform.Qty5.value)
document.quoteform.Price5.value = "\$" + document.quoteform.Price5.value}
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)}
Code #2:

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(Qty,Price,Extension,GrandTotal){
with(GrandTotal)value=0;
for(var i=0;i<Extension.length;i++){
if(!(Qty[i].value=='' || Price[i].value=='')){
with(Qty[i])value=number(value);
with(Extension[i]){
value=Qty[i].value*number(Price[i].value);
GrandTotal.value-= -value;
value=dollar(value);
};
with(Price[i])value=dollar(value);
}
};
with(GrandTotal)value=dollar(value);
};
By the way, the reason formula #1 does not work is because of the use of the "\$". #2 was the workaround for that, but created the new problem. My PHP script is ridiculously long and I don't think that it is where I should be looking to fix this. Is there a way to use #2 without having every field be the same? (i.e. in #1 I have Qty, Qty2, Qty3, etc. Price, Price2, etc....)

*Credit goes to ca_redwards (http://www.codingforums.com/member.php?u=5006) for formula #2. Our original thread is HERE (http://www.codingforums.com/showthread.php?t=62828) , but this is a new problem so I posted a new thread...sorry if that was the wrong thing to do.

By the way, if you want to see the what the output looks like, you can enter your own email into either or both of the email fields. One is the sentto, the other is the carboncopy.

sftl99
07-06-2005, 08:22 PM
var recNo = 5;

function round(number) {
return Math.round(number*100) / 100;
}
function dosum(n) {
var extension = document.getElementById("Extension" + n);
var price = document.getElementById("Price" + n);
var qty = document.getElementById("Qty" + n);

vp = new Number(price.value.replace('\$',''));

extension.value = "\$" + round(vp * qty.value);
price.value = "\$" + vp;
grandtotal();
}
function grandtotal() {
var i, total, tmpI, tmp;

total = 0;

for(i=0;i<recNo;i++){
tmpI = document.getElementById("Extension" + (i + 1));
tmp = new Number(tmpI.value.slice(1));
total += tmp;
}

gt = document.getElementById("GrandTotal");
gt.value = "\$" + total;
}

Then I just change each input to onBlur=dosum(1), dosum(2), etc.

Now I am able to keep each input name different so the PHP works and the function works as well!