Solved

How do I write a Shopping Cart Program in JavaScript?

Posted on 2010-11-16
10
991 Views
Last Modified: 2012-05-10
I am supposed to write a JavaScript program.

The site should have 3 pages: a homepage, a product page, and an order form.

The homepage should include a link to the product page.

The product page should display products and include a product ID, a picture, a price, and description of each product .

This is where I am getting stuck. I have chosen my pictures and products to use, but I can't get the JavaScript program to incorporate them.

Any idea what I could be doing wrong or how to incorporate pictures that I am saving on my pc into the JavaScript program?

Here is what I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
      <title>Shopping Cart</title>
      <meta name="generator" content="CSE HTML Validator Professional (http://www.htmlvalidator.com/)" />
      
      
</head>

      <title>Shopping Cart - Index</title>
      
      <script src="photos.js" type="text/javascript" charset="utf-8<>/scrpt>
      
      <script type= "text/javascript" charset="utf-8">
            window.onload = function ( ) {
                  var holder = document.getElementById( "thumb_holder" );
                  
                  for( var x = 0; x < photos.length; x++ ) {
                        var image = document.createElement( "img" );
                        image.src = "images/" + photos[ x ].name + "_thumb.jpg";
                        var a = document.createElement( "a" );
                        a.href = "JoniQuickShoppingCart.html?image=" + photos[ x ].name;
                        
                        a.appendChild( image );
                        holder.appendChild( a );
                  }
            
            }
      </script>

      
</head>

</body>

      <h1>Shopping Cart - Index</h1>
      
      <div id="thumb_holder"></div>

      

</body>      

</html>
 
0
Comment
Question by:jbosarg
  • 5
  • 5
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34149792
place an alert in your loop to check :


for( var x = 0; x < photos.length; x++ ) {
alert("photo number " + x + " name's : " + photos[ x ].name);
                        var image = document.createElement( "img" );
                        image.src = "images/" + photos[ x ].name + "_thumb.jpg";
                        var a = document.createElement( "a" );
                        a.href = "JoniQuickShoppingCart.html?image=" + photos[ x ].name;
                        
                        a.appendChild( image );
                        holder.appendChild( a );
                  }

Open in new window

0
 

Author Comment

by:jbosarg
ID: 34150290
The alert isn't even showing up when I do that. I will have to take a closer look and get back with you.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34150311
it seems photos is empty or not from this world ;-)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:jbosarg
ID: 34176653
ok. I finally figured out the problem. I just started over from scratch. Now I have a new problem. I want an alert to come up if the user doesn't choose to order something. The problem I am having is that the alert keeps coming up regardless of if something is ordered or not. Can you take a look and see if you can help?
Balloons.html
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34177147
based on subtotal it work fine for me, you may replace :

document.getElementById( "subtotal" ).value

by :

document.getElementById( "subtotal" ).value.length == 0

or :

subtotal = parseFloat(document.getElementById( "subtotal" ).value);
isNaN(subtotal) || subtotal == 0
0
 

Author Comment

by:jbosarg
ID: 34177152
Never mind. I figured it out by adding a calculate function.
0
 

Author Comment

by:jbosarg
ID: 34177332
I have just one more question: If I am supposed to create multiple pages (home page, product page, and order form), how do I link these pages together? More specifically, on the above example, how do I have the user redirected to a page requesting shipping and payment information once the "Place Order" button is clicked.

This is my last question. I promise. :-)
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34177363
each time you submit a form you go on the page specified by the ACTION attribute of the form
from home page you just need an anchor
<a href="Balloons.html">Order Balloons</a>

Open in new window

and
<a href="Products.html" >Products</a>

Open in new window

0
 

Author Comment

by:jbosarg
ID: 34177424
I had it close, but kept screwing something up. You are awesome!!! Thanks!!!!!!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34177455
You're welcome! Thanks for the points! Have a huge and funny week-end!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a business owner, there are many things that keep you up at night. Profit margins, employee retention, human resource protocols, whether your product or service will remain competitive. When you own or manage a technology company that operates la…
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question