Link to home
Start Free TrialLog in
Avatar of bogie
bogie

asked on

Javascript Order Calculator

I'm looking for a JavaScript order form calculator that will automatically put in the price of an item if only the item is chosen from a drop down box.  Then the only other variable would be the qty. This would create the total for that line item in an order.  Each line could be another item
selected from another drop down box.  All items would then go to a sub-total, tax, shipping and grand total. I saw one once like this where the item designated the price instead of a fixed price per line - just can't remember where.

Thanks in advance.
--
Herb Bogart
hbogie@flash.net
ASKER CERTIFIED SOLUTION
Avatar of kollegov
kollegov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kollegov
kollegov

This solution have a difference from previous one
it a little bit easier to change number of lines in form,
And you need to make changes (add/remove items)
in only one place.

<html>
<head>
<script>
// number of lines you want to have;
var nl=3;

function initArray()
{this.length = 0;}

prices  =new initArray();
products=new initArray();

function addElement(pname,pprice)
{oldlength=products.length;
 prices.length++;
 products.length++;
 products[oldlength]=pname;
 prices[oldlength]=pprice;
}

//-----------------------
// add/remove items here!
//-----------------------
addElement("--------select from list----------","0");
addElement("itemname1",1.99);
addElement("itemname2",2.99);
addElement("itemname3",3.00);
addElement("itemname4",4.00);

//-----------------------------------------------------
// you can change change taxes and shipment rules here
// ----------------------------------------------------
function taxesandshipment(totalsumm,itemsnumber)
{return totalsumm*1.05+itemsnumber*0.30;
}


function calc()
{var summ=0;
 var totalqty=0;
 for(i=0;i<nl;i++)
  {var qty;
   eval('qty=document.forms[0].q'+i+'.value');
   for(j=0;j<qty.length;j++)
     {if((qty.charAt(j)<"0") || (qty.charAt(j)>"9"))
            {qty="0";
             eval('document.forms[0].q'+i+'.value="0"');
             alert("Invalid Qty in Line:"+i);
             break;            
            }
     }
   var inx;
   eval('inx=document.forms[0].line'+i+'.selectedIndex');
   var p=prices[inx];
   var t=qty*p;
   if(p!=0) totalqty+=qty;
   eval('document.forms[0].t'+i+'.value=t');
   summ+=t;  
  }
 
 document.forms[0].total.value=Math.floor(summ*100)/100;
 tfinal=taxesandshipment(summ,totalqty);
 document.forms[0].tfinal.value=Math.floor(tfinal*100)/100;
}


function setform()
{var s ='<FORM onSubmit="return false">';
 document.writeln(s);
 for(i=0;i<nl;i++)
  {s='<SELECT name="line'+i+'" onChange="calc()">';
   document.writeln(s);
      for(j=0;j<products.length;j++)
         {s ='<option value="'+prices[j]+'">'+products[j];
          document.writeln(s);
         }
   s ='</select>';
   s +=' Qty:<INPUT name="q'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0">'
   s +=' $:<INPUT name="t'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0"><br>'
   document.writeln(s);
  }
 s='<input type="submit" onClick="calc(); return false;" value="Calculate">';
 document.writeln(s);
 s ='<br>total:<br><input type="text" name="total" size=12 value="" onChange="calc()">'
 s+='<br>including 5% tax and $0.30 shipping and handling per item:<br><input type="text" name="tfinal" size=12 value="" onChange="calc()">'
 s+='</form>';
 document.writeln(s);
 for(i=0;i<nl;i++) eval('document.forms[0].line'+i+'.options[0].selected=true');
}
</script>



</head>
<body>
 <script>
   setform();
 </script>
</body>
</html>
Avatar of bogie

ASKER

Adjusted points to 250