Important alert: (current site time 7/15/2013 1:58:17 PM EDT)
 

VB icon

Build A Dynamic Interactive Price List From A Database

Email
Submitted on: 6/2/2000 9:56:48 AM
By: Ary 
Level: Intermediate
User Rating: By 2 Users
Compatibility: JavaScript
Views: 22817
(About the author)
 
     Builds an interactive product price calculator from a database. Uses Server-side VBScript and Client-side JavaScript.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1860&lngWId=14'for details.'**************************************

<%@ Language=VBScript %>
<!--#include file="../db.inc"-->
<%
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 = "€"
	end select
%>
<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>  Price Calculator</STRONG></FONT>
</TD>
</TABLE>
<!-- Header End-->
<FORM NAME="PriceForm" METHOD="get" ACTION="orderupdate.asp">
<INPUT TYPE="hidden" NAME="ProdID" value="<% Response.Write(Request.QueryString("ProdID")) %>">
<INPUT TYPE="hidden" NAME="Currency" value="<% Response.Write(Request.QueryString("Currency")) %>">
<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><% Response.Write(SoftTitle) %></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 (<% Response.Write(PriceIcon) %>)</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>
<%
If Not rs.BOF And Not rs.EOF Then
	For intRecord = 0 To rs.RecordCount - 1
%>
	
<TR>
	<INPUT TYPE="hidden" NAME="P<% Response.Write(rs.Fields("code")) %>" value="<% Response.Write(rs.Fields(PriceType)) %>">
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><FONT COLOR="#000066"><% Response.Write(rs.Fields("code")) %></FONT></TD>
	<TD WIDTH="60%" VALIGN="top" ALIGN="left"><FONT COLOR="#000000"><STRONG><% Response.Write(rs.Fields("package")) %></STRONG></FONT></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="left"><FONT COLOR="#000066"><% Response.Write(rs.Fields(PriceType)) %></FONT></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><INPUT NAME="Q<% Response.Write(rs.Fields("code")) %>" SIZE="4" Value="0" onChange="S<% Response.Write(rs.Fields("code")) %>()"></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><INPUT NAME="T<% Response.Write(rs.Fields("code")) %>" SIZE="8" Value="0.00"></TD>
</TR>
	
<%
		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
%>
<TR><TD COLSPAN="5"><HR COLOR="#000066"></TD></TR>
<%
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
%>
	
<TR>
	<INPUT TYPE="hidden" NAME="P<% Response.Write(rsAdd.Fields("code")) %>" value="<% Response.Write(rsAdd.Fields(PriceType)) %>">
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><FONT COLOR="#000066"><% Response.Write(rsAdd.Fields("code")) %></FONT></TD>
	<TD WIDTH="60%" VALIGN="top" ALIGN="left"><FONT COLOR="#000000"><STRONG><% Response.Write(rsAdd.Fields("package")) %></STRONG></FONT></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="left"><FONT COLOR="#000066"><% Response.Write(rsAdd.Fields(PriceType)) %></FONT></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><INPUT NAME="Q<% Response.Write(rsAdd.Fields("code")) %>" SIZE="4" Value="0" onChange="S<% Response.Write(rsAdd.Fields("code")) %>()"></TD>
	<TD WIDTH="10%" VALIGN="top" ALIGN="center"><INPUT NAME="T<% Response.Write(rsAdd.Fields("code")) %>" SIZE="8" Value="0.00"></TD>
</TR>
	
<%	
		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
%>
<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"></TD>
</TR>
<TR><TD COLSPAN="5"><HR COLOR="#000066"></TD></TR>
<%
sSQL = "SELECT * FROM items WHERE code = 'SER03'"
rsPP.Open sSQL,db,adOpenKeySet,adLockReadOnly
If Not rsPP.BOF And Not rsPP.EOF Then rsPP.movefirst
%>
<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"><% Response.Write(PriceIcon & rsPP.Fields(PriceType)) %></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"></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"></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"></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>
<%
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)
%>
</BODY>
</HTML>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/9/2004 10:39:12 AMBrian Battles WS1O

Do you have a functioning demo of this with the include file(s) and database?
(If this comment was disrespectful, please report it.)

 
11/13/2007 6:07:04 PMYakub

YOUR CODE IS NICE KEEP IT UP
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.