Help with shopping cart problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randi

    Help with shopping cart problem

    Hi, Thanks David and I got that to work, I guess I still need to get it to
    work using the arrays and the selectedIndex property. I have to use an
    alert box that shows the total price like: The cost = the base price + RAM
    price + Hard Drive Price + Operating System Price The cost = the base price
    + RAM price + Hard Drive Price + Operating System Price. This is what I
    have so far, 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="JavaS cript">
    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){

    }



    </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%"><di v 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>Windo ws XP Home Edition</option>
    <option>Windo ws XP Professional Edition (add $70)</option>
    <option>Windo ws XP Home Edition with Microsoft Plus (add $20)</option>
    <option>Windo ws XP Home Professional Edition with Microsoft Plus (add
    $90)</option>
    </select>
    </p>
    <p>Enter your name here:
    <input type="text" name="customerN ame">
    </p>
    <p>
    <input type="submit" name="Submit" value="Price">
    </p>
    <p>&nbsp;</p>
    </form>
    <p>&nbsp;</p>
    </body>
    </html>


  • Michael Winter

    #2
    Re: Help with shopping cart problem

    On Sat, 03 Apr 2004 13:32:58 GMT, Randi <RSaddler@stny. rr.com> wrote:
    [color=blue]
    > Hi, Thanks David and I got that to work,[/color]

    Good...
    [color=blue]
    > I guess I still need to get it to work using the arrays and the
    > selectedIndex property.[/color]

    ....so I don't see why you're trying to pursue another course of action.
    [color=blue]
    > I have to use an alert box[/color]

    Why? Do you have something against people that don't use JavaScript?

    All of this would be best performed on a server anyway, using a
    centralised database and guaranteed execution. Using JavaScript means
    updating multiple pages if you alter prices (which you are sure to do when
    selling computer equipment), and needlessly excluding potential customers
    that don't use JavaScript.
    [color=blue]
    > that shows the total price like: The cost = the base price + RAM price
    > + Hard Drive Price + Operating System Price [snipped 2nd instance][/color]

    So you just duplicate what David suggested for the other price modifiers.
    [color=blue]
    > This is what I have so far, I think i need to use a for loop, because of
    > the array.[/color]

    As you don't need the array, you don't need the loop. However, this is how
    you could do it:

    var base = 799;
    var RAM = [ 0, 70, 150, 280 ];
    var HD = [ 0, 30, 70 ];
    var OS = [ 0, 70, 150, 280 ];

    function calcPrice() {
    var price = base;
    var form = document.forms[ 'myForm' ];

    price += RAM[ form.elements[ 'RAM' ].selectedIndex ];
    price += HD[ form.elements[ 'HardDrive' ].selectedIndex ];
    price += OS[ form.elements[ 'OS' ].selectedIndex ];

    // "price" now contains the modified total
    }

    [snipped code]

    From your original post:

    <form name="myForm" method="post" onSubmit="Calc( myForm)">

    This won't work on a lot of browsers. Only a few browsers allow you to use
    id and name attribute values as references to the respective elements. Use
    either the well supported collections (forms, images, etc) or DOM methods
    to access elements. However, in this case, you simply need to use

    <form ... onsubmit="Calc( this)">

    which will pass a reference to the form to the Calc() function.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    Working...