Please visit our sponsor
UNKNOWN //************************************** // Name: Build A Dynamic Interactive Price List From A Database // Description:Builds an interactive product price calculator from a database. Uses Server-side VBScript and Client-side JavaScript. // By: Ary // // // Inputs:Product Type Currency Used // // Returns:Products Selected Overall Price (so that it can then accept credit card details, etc) // //Assumes:This code will not work straight away, it is mainly to explain haw it's done. // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.1860/lngWId.2/qx/vb/scripts/ShowCode.htm //for details. //************************************** &lt;%@ Language=VBScript %&gt; <!--#include file="../db.inc"--> &lt;% Response.Buffer=true Response.Expires=Now()+1 Const adOpenKeySet = 1 Const adLockReadOnly = 1 strScriptName = Request.ServerVariables("SCRIPT_NAME") Set rsAdd = Server.CreateObject("ADODB.Recordset") Set rs = Server.CreateObject("ADODB.Recordset") Set rsPP = Server.CreateObject("ADODB.Recordset") // 'Get table to work on Dim SoftTitle Select Case Request.QueryString("ProdID") Case "CHK": SoftTitle = "Socks" Case "DBI": SoftTitle = "Trousers" Case "NOT": SoftTitle = "Shirts" Case "PRO": SoftTitle = "skirts" Case "RET": SoftTitle = "dresses" Case "TRK": SoftTitle = "shorts" Case "TRS": SoftTitle = "t-shirts" End Select sSQL = "SELECT * FROM items WHERE type = '" & Request.QueryString("ProdID") & "'" rs.open sSQL,db,adOpenKeySet,adLockReadOnly ' Get currency to use dim PriceType Dim PriceIcon Select Case Request.QueryString("Currency") Case "UK" PriceType = "uk" PriceIcon = "£" Case "US" PriceType = "us" PriceIcon = "$" Case "EU" PriceType = "euro" PriceIcon = "&#8364;" end select %&gt; <html> <head> <title>Build Price Calculator From Database</title> <style> BODY{font-family: Verdana, Arial, sans-serif; font-size: 10pt;} TABLE{font-family: Verdana, Arial, sans-serif; font-size: 8pt;} H1{font-family: Verdana, Arial, sans-serif; font-size: 24pt;} H2{font-family: Verdana, Arial, sans-serif; font-size: 20pt;} H3{font-family: Verdana, Arial, sans-serif; font-size: 14pt;} H4{font-family: Verdana, Arial, sans-serif; font-size: 12pt;} </style> <script language="javascript"> function roundOff(value, precision) { value = "" + value precision = parseInt(precision); var whole = "" + Math.round(value * Math.pow(10, precision)); var decPoint = whole.length - precision; if(decPoint != 0) { result = whole.substring(0, decPoint); result += "."; result += whole.substring(decPoint, whole.length); } else { result = whole; } return result; } </script> </head> <body bgcolor="#FFFFFF" text="#000000" link="#333399" alink="#00FFFF" vlink="#339999"> <div align="center"> <!-- Header Start--> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td bgcolor="#3399CC"> <font color="#FFFFFF" size="5"><strong>&nbsp;&nbsp;Price Calculator</strong></font> </td> </table> <!-- Header End--> <form name="PriceForm" method="get" action="/"> <input type="hidden" name="ProdID" value="<% Response.Write(Request.QueryString(" prodid")) %>"&gt; <input type="hidden" name="Currency" value="<% Response.Write(Request.QueryString(" currency")) %>"&gt; <table cellspacing="1" cellpadding="1" border="0" width="100%"> <tr bgcolor="#000066"> <td bgcolor="#6699CC" width="100%" colspan="5"> <center><font size="4" color="#FFFFFF"><strong>&lt;% Response.Write(SoftTitle) %&gt;</strong></font></center> </td> </tr> <tr bgcolor="#D0D0D0"> <td width="10%" align="center"><font size="2" color="#000066"><strong>Item Code</strong></font></td> <td width="60%" align="center"><font size="2" color="#000066"><strong>Product</strong></font></td> <td width="10%" align="center"><font size="2" color="#000066"><strong>Unit Price (&lt;% Response.Write(PriceIcon) %&gt;)</strong></font></td> <td width="10%" align="center"><font size="2" color="#000066"><strong>Units</strong></font></td> <td width="10%" align="center"><font size="2" color="#000066"><strong>Total Amount</strong></font></td> </tr> &lt;% If Not rs.BOF And Not rs.EOF Then For intRecord = 0 To rs.RecordCount - 1 %&gt; <tr> <input type="hidden" name="P<% Response.Write(rs.Fields(" code")) %>" value="&lt;% Response.Write(rs.Fields(PriceType)) %&gt;"&gt; <td width="10%" valign="top" align="center"><font color="#000066">&lt;% Response.Write(rs.Fields("code")) %&gt;</font></td> <td width="60%" valign="top" align="left"><font color="#000000"><strong>&lt;% Response.Write(rs.Fields("package")) %&gt;</strong></font></td> <td width="10%" valign="top" align="left"><font color="#000066">&lt;% Response.Write(rs.Fields(PriceType)) %&gt;</font></td> <td width="10%" valign="top" align="center"><input name="Q<% Response.Write(rs.Fields(" code")) %>" SIZE="4" Value="0" onChange="S&lt;% Response.Write(rs.Fields("code")) %&gt;()"&gt;</td> <td width="10%" valign="top" align="center"><input name="T<% Response.Write(rs.Fields(" code")) %>" SIZE="8" Value="0.00"&gt;</td> </tr> &lt;% Response.Write("<script language="JavaScript"> ") Response.Write("function S" & rs.Fields("code").Value & "() { ") Response.Write("document.PriceForm.T" & rs.Fields("code").Value & ".value = roundOff(eval(document.PriceForm.Q" & rs.Fields("code").Value & ".value * document.PriceForm.P" & rs.Fields("code").Value & ".value),2); ") Response.Write("updateTotals(); ") Response.Write(" } ") Response.Write("</script>") rs.MoveNext Next End If %&gt; <tr><td colspan="5"><hr color="#000066"></td></tr> &lt;% sSQL = "SELECT * FROM items WHERE type = 'CON'" rsAdd.Open sSQL,db,adOpenKeySet,adLockReadOnly If Not rsAdd.BOF And Not rsAdd.EOF Then For intRecord = 0 To rsAdd.RecordCount - 1 %&gt; <tr> <input type="hidden" name="P<% Response.Write(rsAdd.Fields(" code")) %>" value="&lt;% Response.Write(rsAdd.Fields(PriceType)) %&gt;"&gt; <td width="10%" valign="top" align="center"><font color="#000066">&lt;% Response.Write(rsAdd.Fields("code")) %&gt;</font></td> <td width="60%" valign="top" align="left"><font color="#000000"><strong>&lt;% Response.Write(rsAdd.Fields("package")) %&gt;</strong></font></td> <td width="10%" valign="top" align="left"><font color="#000066">&lt;% Response.Write(rsAdd.Fields(PriceType)) %&gt;</font></td> <td width="10%" valign="top" align="center"><input name="Q<% Response.Write(rsAdd.Fields(" code")) %>" SIZE="4" Value="0" onChange="S&lt;% Response.Write(rsAdd.Fields("code")) %&gt;()"&gt;</td> <td width="10%" valign="top" align="center"><input name="T<% Response.Write(rsAdd.Fields(" code")) %>" SIZE="8" Value="0.00"&gt;</td> </tr> &lt;% Response.Write("<script language="JavaScript"> ") Response.Write("function S" & rsAdd.Fields("code").Value & "() { ") Response.Write("document.PriceForm.T" & rsAdd.Fields("code").Value & ".value = roundOff(eval(document.PriceForm.Q" & rsAdd.Fields("code").Value & ".value * document.PriceForm.P" & rsAdd.Fields("code").Value & ".value),2); ") Response.Write("updateTotals(); ") Response.Write(" } ") Response.Write("</script>") rsAdd.MoveNext Next End If %&gt; <tr><td colspan="5"><hr color="#000066"></td></tr> <tr> <td width="70%" align="left" colspan="2"><font size="2" color="#000066"><strong>Support & Maintenance:</strong></font><br> <font size="1" color="#000066"><i>Support and maintenance costs 15% of the prevailing licence price per annum and is mandatory for the first year. The maintenance charge for additional licences is pro-rated to the contract renewal date.</i></font></td> <td width="20%" align="right" colspan="2"></td> <td width="10%" align="center"><input name="Support" size="8" value="<% Response.Write(" 0.00") %>" ALIGN="right"&gt;</td> </tr> <tr><td colspan="5"><hr color="#000066"></td></tr> &lt;% sSQL = "SELECT * FROM items WHERE code = 'SER03'" rsPP.Open sSQL,db,adOpenKeySet,adLockReadOnly If Not rsPP.BOF And Not rsPP.EOF Then rsPP.movefirst %&gt; <input type="hidden" name="PP" value="<% Response.Write(rsPP.Fields(PriceType)) %>"> <tr> <td width="70%" align="left" colspan="2"><font size="2" color="#000066"><strong>Postage & Packing:</strong></font><br> <font size="1" color="#000066"><i>Postage and packing is mandatory on all orders.</i></font></td> <td width="20%" align="right" colspan="2"></td> <td width="10%" align="center">&lt;% Response.Write(PriceIcon & rsPP.Fields(PriceType)) %&gt;</td> </tr> <tr><td colspan="5"><hr color="#000066"></td></tr> <tr> <td width="90%" align="right" colspan="4"><font size="2" color="#000066"><strong>Total:</strong></font></td> <td width="10%" align="center"><input name="Gross" size="8" value="<% Response.Write(" 0.00") %>" ALIGN="right"&gt;</td> </tr> <tr> <td width="90%" align="right" colspan="4"><font size="2" color="#000066"><strong>VAT (17.5%):</strong></font></td> <td width="10%" align="center"><input name="Tax" size="8" value="<% Response.Write(" 0.00") %>" ALIGN="right"&gt;</td> </tr> <tr> <td class="3dRaised" bgcolor="#6699CC" width="90%" align="right" colspan="4"><font size="2" color="#FFFFFF"><strong>Final Total:</strong></font></td> <td width="10%" align="center"><input name="TotalValue" size="8" value="<% Response.Write(" 0.00") %>" ALIGN="right"&gt;</td> </tr> <tr bgcolor="#FFFFFF"> <td width="100%" align="right" colspan="5"><br><br> <input type="reset" name="reset1" value="Begin Again" style="BACKGROUND:#6699CC;COLOR:#FFFFFF"> <input type="submit" name="submit" value="Add To Order" style="BACKGROUND:#6699CC;COLOR:#FFFFFF"> </td> </tr> </table> </div> </form> &lt;% Response.Write("<script language="JavaScript"> " + VBCRLF) Response.Write("function updateTotals() { " + VBCRLF) Response.Write("var SoftTotal;" + VBCRLF) Response.Write("var AddTotal;" + VBCRLF) Response.Write("var TaxTotal;" + VBCRLF) Response.Write("var SupportTotal;" + VBCRLF) Response.Write("var GrossTotal;" + VBCRLF) Response.Write("var PPTotal;" + VBCRLF) Response.Write("var FullTotal;" + VBCRLF) Response.Write("SoftTotal = ") rs.MoveFirst For intRecord = 0 To rs.RecordCount - 1 Response.Write("eval(document.PriceForm.T" & rs.Fields("code").Value & ".value") If Not intRecord = rs.RecordCount - 1 Then Response.Write(") + ") Else Response.Write("); " + VBCRLF) End If rs.MoveNext Next Response.Write("AddTotal = ") rsAdd.MoveFirst For intRecord = 0 To rsAdd.RecordCount - 1 Response.Write("eval(document.PriceForm.T" & rsAdd.Fields("code").Value & ".value") If Not intRecord = rsAdd.RecordCount - 1 Then Response.Write(") + ") Else Response.Write("); " + VBCRLF) End If rsAdd.MoveNext Next Response.Write("PPTotal = eval(document.PriceForm.PP.value); " + VBCRLF) Response.Write("SupportTotal = eval(SoftTotal / 100) * 15; " + VBCRLF) Response.Write("document.PriceForm.Support.value = roundOff(SupportTotal,2); " + VBCRLF) Response.Write("GrossTotal = SoftTotal + AddTotal + SupportTotal + PPTotal; " + VBCRLF) Response.Write("document.PriceForm.Gross.value = roundOff(GrossTotal,2); " + VBCRLF) Response.Write("TaxTotal = eval(document.PriceForm.Gross.value / 100) * 17.5; " + VBCRLF) Response.Write("document.PriceForm.Tax.value = roundOff(TaxTotal,2); " + VBCRLF) Response.Write("FullTotal = TaxTotal + GrossTotal; " + VBCRLF) Response.Write("document.PriceForm.TotalValue.value = roundOff(FullTotal,2); " + VBCRLF) Response.Write("} " + VBCRLF) Response.Write(" </script>" + VBCRLF) %&gt; </body> </html>