473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple shopping cart problem

Hi, I am trying to write this program to get this shopping cart to work. I
need to use the calc function to determine the option prices, using the
selectedIndex property for each dropdown list. This is what I have so far,
I started with if statements, but I think i need to use a for loop, because
of the array. Any help would be appreciated. The code I have so far is
below.

Thanks,

Randi

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Harmon's Computer Store</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
var basePrice = 799;

var Ram = new Array(0, 70, 150, 280);
var HD = new Array(0, 30, 70);
var OS = new Array(0, 70, 150, 280);

function Calc(myForm){
if (myform.RAM.selectedIndex=0){
price = basePrice;
else if
(myform.RAM.selectedIndex=1){
price = basePrice + 70;}
else if
(myform.RAM.selectedIndex=2){
price = basePrice + 150;}
else if
(myform.RAM.selectedIndex=3){
price = basePrice + 280;}
}

</script>
</head>

<body>
<p align="center"><font color="#FF0000" size="+2" face="Arial, Helvetica,
sans-serif">Harmon's Computer Store</font></p>
<table width="75%" border="0">
<tr>
<td width="35%"><div align="center"><img src="images.jpg" width="103"
height="90"></div></td>
<td width="65%">The base price of this computer is $799. Because this is
a spring break
special, the manufacturer offers limited options.</td>
</tr>
</table>
<p>&nbsp;</p>
<p>Intel Pentium 4 300 GHz , 128 MB RAM, 40 G Hard Drive, 48x/24x/48 CD RW,
17&quot;
color monitor.</p>
<form name="myForm" method="post" onSubmit="Calc(myForm)">
<p>Optional Upgrades</p>
<p>
<select name="RAM">
<option>128 MB</option>
<option>256 MB DDR(add $70)</option>
<option>512 MB DDR (add $150)</option>
<option>1 GM DDR (add $280)</option>
</select>
</p>
<p>
<select name="HardDrive">
<option> 40 GB</option>
<option>80 GB (add $30)</option>
<option>120 GB (add $70) </option>
</select>
</p>
<p>
<select name="OS">
<option>Windows XP Home Edition</option>
<option>Windows XP Professional Edition (add $70)</option>
<option>Windows XP Home Edition with Microsoft Plus (add $20)</option>
<option>Windows XP Home Professional Edition with Microsoft Plus (add
$90)</option>
</select>
</p>
<p>Enter your name here:
<input type="text" name="customerName">
</p>
<p>
<input type="submit" name="Submit" value="Price">
</p>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>

Jul 23 '05 #1
3 1431
First suggestion, change your select tags as follows:

<select name="RAM" onChange="Calc();">
<option value="0.00">128 MB</option>
<option value="70.00">256 MB DDR(add $70)</option>
<option value="150.00">512 MB DDR (add $150)</option>
<option value="280.00">1 GM DDR (add $280)</option>
</select>

This will remove the need for your arrays.

Second, add an input box somewhere as follows:

<input name=price onFocus="blur();">

Third, change your Calc() function as follows:

function Calc() {
basePrice += parseFloat(myform.RAM.value);
basePrice += parseFloat(myform.HardDrive.value);
basePrice += parseFloat(myform.OS.value);

myform.Price.value = basePrice;
}

You will probably need to format the price to pad extra zero's so $120
looks like $120.00.

Hope this helps.

Randi wrote:
Hi, I am trying to write this program to get this shopping cart to work. I
need to use the calc function to determine the option prices, using the
selectedIndex property for each dropdown list. This is what I have so far,
I started with if statements, but I think i need to use a for loop, because
of the array. Any help would be appreciated. The code I have so far is
below.

Thanks,

Randi

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Harmon's Computer Store</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
var basePrice = 799;

var Ram = new Array(0, 70, 150, 280);
var HD = new Array(0, 30, 70);
var OS = new Array(0, 70, 150, 280);

function Calc(myForm){
if (myform.RAM.selectedIndex=0){
price = basePrice;
else if
(myform.RAM.selectedIndex=1){
price = basePrice + 70;}
else if
(myform.RAM.selectedIndex=2){
price = basePrice + 150;}
else if
(myform.RAM.selectedIndex=3){
price = basePrice + 280;}
}

</script>
</head>

<body>
<p align="center"><font color="#FF0000" size="+2" face="Arial, Helvetica,
sans-serif">Harmon's Computer Store</font></p>
<table width="75%" border="0">
<tr>
<td width="35%"><div align="center"><img src="images.jpg" width="103"
height="90"></div></td>
<td width="65%">The base price of this computer is $799. Because this is
a spring break
special, the manufacturer offers limited options.</td>
</tr>
</table>
<p>&nbsp;</p>
<p>Intel Pentium 4 300 GHz , 128 MB RAM, 40 G Hard Drive, 48x/24x/48 CD RW,
17&quot;
color monitor.</p>
<form name="myForm" method="post" onSubmit="Calc(myForm)">
<p>Optional Upgrades</p>
<p>
<select name="RAM">
<option>128 MB</option>
<option>256 MB DDR(add $70)</option>
<option>512 MB DDR (add $150)</option>
<option>1 GM DDR (add $280)</option>
</select>
</p>
<p>
<select name="HardDrive">
<option> 40 GB</option>
<option>80 GB (add $30)</option>
<option>120 GB (add $70) </option>
</select>
</p>
<p>
<select name="OS">
<option>Windows XP Home Edition</option>
<option>Windows XP Professional Edition (add $70)</option>
<option>Windows XP Home Edition with Microsoft Plus (add $20)</option>
<option>Windows XP Home Professional Edition with Microsoft Plus (add
$90)</option>
</select>
</p>
<p>Enter your name here:
<input type="text" name="customerName">
</p>
<p>
<input type="submit" name="Submit" value="Price">
</p>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>


Jul 23 '05 #2
Oops, one error. In your function, you have to reset basePrice back to
799 or your Calc() method will grow indefinitely. See below for change....

David Morris wrote:
First suggestion, change your select tags as follows:

<select name="RAM" onChange="Calc();">
<option value="0.00">128 MB</option>
<option value="70.00">256 MB DDR(add $70)</option>
<option value="150.00">512 MB DDR (add $150)</option>
<option value="280.00">1 GM DDR (add $280)</option>
</select>

This will remove the need for your arrays.

Second, add an input box somewhere as follows:

<input name=price onFocus="blur();">

Third, change your Calc() function as follows:

function Calc() { basePrice = 799; basePrice += parseFloat(myform.RAM.value);
basePrice += parseFloat(myform.HardDrive.value);
basePrice += parseFloat(myform.OS.value);

myform.Price.value = basePrice;
}

You will probably need to format the price to pad extra zero's so $120
looks like $120.00.

Hope this helps.

Randi wrote:
Hi, I am trying to write this program to get this shopping cart to
work. I
need to use the calc function to determine the option prices, using the
selectedIndex property for each dropdown list. This is what I have so
far,
I started with if statements, but I think i need to use a for loop,
because
of the array. Any help would be appreciated. The code I have so far is
below.

Thanks,

Randi

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Harmon's Computer Store</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
var basePrice = 799;

var Ram = new Array(0, 70, 150, 280);
var HD = new Array(0, 30, 70);
var OS = new Array(0, 70, 150, 280);

function Calc(myForm){
if (myform.RAM.selectedIndex=0){
price = basePrice;
else if
(myform.RAM.selectedIndex=1){
price = basePrice + 70;}
else if
(myform.RAM.selectedIndex=2){
price = basePrice + 150;}
else if
(myform.RAM.selectedIndex=3){
price = basePrice + 280;}
}

</script>
</head>

<body>
<p align="center"><font color="#FF0000" size="+2" face="Arial, Helvetica,
sans-serif">Harmon's Computer Store</font></p>
<table width="75%" border="0">
<tr>
<td width="35%"><div align="center"><img src="images.jpg" width="103"
height="90"></div></td>
<td width="65%">The base price of this computer is $799. Because
this is
a spring break
special, the manufacturer offers limited options.</td>
</tr>
</table>
<p>&nbsp;</p>
<p>Intel Pentium 4 300 GHz , 128 MB RAM, 40 G Hard Drive, 48x/24x/48
CD RW,
17&quot;
color monitor.</p>
<form name="myForm" method="post" onSubmit="Calc(myForm)">
<p>Optional Upgrades</p>
<p>
<select name="RAM">
<option>128 MB</option>
<option>256 MB DDR(add $70)</option>
<option>512 MB DDR (add $150)</option>
<option>1 GM DDR (add $280)</option>
</select>
</p>
<p>
<select name="HardDrive">
<option> 40 GB</option>
<option>80 GB (add $30)</option>
<option>120 GB (add $70) </option>
</select>
</p>
<p>
<select name="OS">
<option>Windows XP Home Edition</option>
<option>Windows XP Professional Edition (add $70)</option>
<option>Windows XP Home Edition with Microsoft Plus (add $20)</option>
<option>Windows XP Home Professional Edition with Microsoft Plus (add
$90)</option>
</select>
</p>
<p>Enter your name here:
<input type="text" name="customerName">
</p>
<p>
<input type="submit" name="Submit" value="Price">
</p>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>


Jul 23 '05 #3
On Fri, 02 Apr 2004 09:56:03 -0500, David Morris
<dm*****@dynamicquest.com> wrote:

[fixed top-post]
David Morris wrote:
First suggestion, change your select tags as follows:

<select name="RAM" onChange="Calc();">
<option value="0.00">128 MB</option>
<option value="70.00">256 MB DDR(add $70)</option>
<option value="150.00">512 MB DDR (add $150)</option>
<option value="280.00">1 GM DDR (add $280)</option>
</select>

This will remove the need for your arrays.

Second, add an input box somewhere as follows:

<input name=price onFocus="blur();">

Third, change your Calc() function as follows:

function Calc() {
basePrice = 799;
basePrice += parseFloat(myform.RAM.value);
basePrice += parseFloat(myform.HardDrive.value);
basePrice += parseFloat(myform.OS.value);

myform.Price.value = basePrice;
}

You will probably need to format the price to pad extra zero's so $120
looks like $120.00.

Read the FAQ for a method of doing this:

<URL:http://jibbering.com/faq/#FAQ4_6>

[snipped OP]
Oops, one error. In your function, you have to reset basePrice back to
799 or your Calc() method will grow indefinitely. See below for
change....


Wouldn't it be a better idea to use a temporary variable during the
calculation, otherwise you start introducing "magic numbers" into the code
and increase maintenance.

function Calc() {
/* basePrice = 799; Don't do this. Use only
the original declaration */

var tmp = basePrice;

tmp += parseFloat(myform.RAM.value);
tmp += parseFloat(myform.HardDrive.value);
tmp += parseFloat(myform.OS.value);

myform.Price.value = tmp.toFixed( 2 );
}

The toFixed() call above assumes that you used the script shown in the
cited FAQ entry.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3778
by: Josef Blösl | last post by:
hi folks! i have a cart link in all of my listed products on the site. when i press this link i will generate a url like this ...
17
2325
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
5
20475
by: VM | last post by:
I'm interested in creating a simple shopping cart in C#. Are there sites that show you, step by step, how to create a simple cart? The sites I've found only show you pre-made shopping carts that...
5
1367
by: =?Utf-8?B?Q2hpV2hpdGVTb3g=?= | last post by:
i just finished creating an asp.net application named booksApp. during the testing phase uploaded live ( ex: www.bookies.com) , i tried to purchase something and add it to my shopping cart. ...
1
1577
by: mishomira | last post by:
Hi ASP masters, I have a shopping cart where customers can buy packages and/or products. so the fist page(packages) customers can buy package and on the second page(products) there is two forms the...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.