# Javascript-Edit Script Calculation on Form



## npd (Jan 16, 2003)

Hi there, was wondering if its possible to edit the following script in a form so that users are required to enter both values for PRICE and QUANTITY instead of just QUANTITY only. Thanks for your help.

--------------------------------------------------------------------------

Order Form Demo


PriceQuantityTotal$$$$$$

Order Total 

 

Tax (MA) 

 

Shipping & Handling 

 

Grand Total 

 


----------



## Shadow2531 (Apr 30, 2001)

You can't edit the prices because of the onfocus="this.blur();" on each of the item price inputs.

If you get rid of that for each, then the user can input prices into those fields.

Is that what you mean?


----------



## npd (Jan 16, 2003)

Yup, thats exactly it. You're a genius. Thanks.


----------



## npd (Jan 16, 2003)

Hi there, just a followup. Is there any way to modify the script so that if subtotal equals less than $50.00 then add $5 to subtotal and output grandtotal $55.00. Any help would be greatly appreciated.


----------



## dmneoblade (Apr 15, 2004)

Change

```
function Total() {
var q, i, ordertotal, tax, sh, total;
ordertotal = 0;
nitems = 6
for (i=1; i<nitems+1; i++) {
eval("document.orderform.Item" + i + "Total.value = '';");
eval("q = document.orderform.Item" + i + "Quantity.value;");
if (q) {
eval("total=document.orderform.Item" + i + "Price.value * document.orderform.Item" + i + "Quantity.value;");
eval("document.orderform.Item" + i + "Total.value=Currency(total)");
eval("ordertotal = ordertotal + total;");
}
}
```
to

```
function Total() 
   {
   var q, i, ordertotal, tax, sh, total;
   ordertotal = 0;
   nitems = 6
   for (i=1; i<nitems+1; i++) 
      {
      eval("document.orderform.Item" + i + "Total.value = '';");
      eval("q = document.orderform.Item" + i + "Quantity.value;");
      if (q) 
         {
         eval("total=document.orderform.Item" + i + "Price.value * document.orderform.Item" + i + "Quantity.value;");
         eval("document.orderform.Item" + i + "Total.value=Currency(total)");
         if(ordertotal <= 50)
            {
            ordertotal += 5;
            }
         eval("ordertotal = ordertotal + total;");
         }
      }
```


----------



## npd (Jan 16, 2003)

Hi there,

I've included the following script in the code. I believe that's the only change. However, I'm having trouble getting the code to work. It's still giving me the total without the $5 increase. Thanks for your help.

if(ordertotal <= 50)
{
ordertotal += 5;
}


----------



## dmneoblade (Apr 15, 2004)

lets try this then...

```
if(ordertotal <= 50)
            {
            ordertotal += 5;
            }
         eval("ordertotal = ordertotal + total;");
         }
```
to

```
if(ordertotal <= 50)
            {
            eval("ordertotal = ordertotal + 5 + total;");
            }
         else
             {
             eval("ordertotal = ordertotal + total;");
             }
         }
```


----------



## npd (Jan 16, 2003)

Nope, it doesn't work. But thanks for trying though.


----------



## dmneoblade (Apr 15, 2004)

Just noticed something in your script. Near the top, change
eval("document.orderform.Item" + i + "Total.value = '';");
to
eval("document.orderform.Item" + i + "Total.value = '');


----------



## npd (Jan 16, 2003)

OK, I'll give it a try


----------



## dmneoblade (Apr 15, 2004)

post your cleaned up source, and I'll see what else I can find. Its a bit hard to read as of now.


----------



## npd (Jan 16, 2003)

Nope, tried it, not working. Thanks for trying though. I give you an E for effort. Thanks.


----------



## Shadow2531 (Apr 30, 2001)

Yes, post cleaned up source.


----------



## npd (Jan 16, 2003)

Will do. Thanks.


----------



## npd (Jan 16, 2003)

OK, this is as clean as it can get, I think. Hope it helps. This is a .js script only. If you also require the additional coding within the body tag, please let me know (or see original post). Thanks guys.


----------



## Shadow2531 (Apr 30, 2001)

If all you want to do is add 5 to the order total (aka subtotal) if it's less than 50, you can do it like this.

Referring to the script in your first post.

In Total()

change

document.orderform.OrderTotal.value = Currency(ordertotal)

to


```
bla = Currency(ordertotal);
fix = bla.substring(1);
check = parseFloat(fix);
if (check < 50) {
    document.orderform.OrderTotal.value = "$"+(check += 5);
}
else {
    document.orderform.OrderTotal.value = Currency(ordertotal);
}
```
You can also do it like this (same thing, just less lines).


```
check = parseFloat(((Currency(ordertotal)).substring(1)));
if (check < 50) {
    document.orderform.OrderTotal.value = "$"+(check += 5);
}
else {
    document.orderform.OrderTotal.value = Currency(ordertotal);
}
```
However, I am not very fond of the way the whole script is setup. It would be better to evaluate everything server-side. (If this is actually used as an ordering form).

Also (referring to your original html), in addition to getting rid of the onfocus attributes, you can also get rid of the onchange attributes and just use onblur="Total()" on all the inputs (besides submit).

Note: To test with the code in your first post, just modify the js. Set if (check < 200) instead of 50. Then if you type 1 for the quantity of $199.95, the subtotal will be 204.95


----------



## dmneoblade (Apr 15, 2004)

You take over shadow, you seem to be more familiar with Javascript than I am.


----------



## npd (Jan 16, 2003)

Hi Shadow,

The script works great. Thanks for all your help.


----------



## Shadow2531 (Apr 30, 2001)

I see

put an alert right after the check = line.

check = parseFloat(((Currency(ordertotal)).substring(1)));
alert(check);

That will show you that check is wrong. Don't know why yet, but I'll check it out.


----------



## Shadow2531 (Apr 30, 2001)

Here's a quick fix until I find a better way.

change the 2 

intstr = temp1+","+temp2

lines

to

intstr = temp1+temp2

(Comma's messing up parseFloat)


----------



## Shadow2531 (Apr 30, 2001)

attached is npd2.txt

Give it a shot.

I reverted the orderTotal code back to the original in Total().

I changed the Currency function to support 1 more argument: is_order_total

So that means any time you call Currency() ,you have to specify the second argument too.

Currency(ordertotal,"yes"); That tells the conditional in Currency to add 5 to the value being passed (if it's < 200). Since 5 is added before the commas get put in, there is no worry there.

In npd2.txt, I have it set for < 200 before it adds 5. You need to change that.

This change might also allow you to add 5 to the Total instead if you want.

Just change Currency(ordertotal,"yes"); to Currency(ordertotal,"no"); and Currency(ordertotal + tax + sh,"no"); to Currency(ordertotal + tax + sh,"yes");

I did not try that last part, but it may work.


----------



## npd (Jan 16, 2003)

OK, I'll give it a shot. I appreciate your time.


----------



## npd (Jan 16, 2003)

Hi, Shadow

You the man. Most of this stuff looks like gibberish to me, but the script works perfectly. I'm simply amazed (yet again). Many thanks.


----------

