用PHP,JavaScript和Html创建一个简单的购物车?

问题描述:

I am attempting to create a shopping cart using PHP for a Shirt Design Website. Users can design the shirts and then add them to the cart. So I created a form, in which users can pick their colour and sizes:

<form action="cart_main.php">
    <table>
        <tr><td>Colour:</td><td> <select>
        <option value="Blue">Blue</option>
        <option value="Red">Red</option>
        <option value="Pink">Pink</option>
        <option value="Black">Black</option>
        <option value="Grey">Grey</option>
        <option value="Beige">Beige</option>
        <option value="White">White</option>
        </select></td></tr>

        <tr><td>Design:</td><td> <select>
        <option value="T1">Skull</option>
        <option value="T2">Liquify</option>
        <option value="T3">Wings</option>
        <option value="T4">Small Skull</option>
        <option value="T5">Cogs</option>
        <option value="T6">Safety Pin</option>
        </select></td></tr>

        <tr><td>Pop Colour: </td><td> <select>
        <option value="Blue">Blue</option>
        <option value="Red">Red</option>            
        <option value="Yellow">Yellow</option>
        <option value="Purple">Purple</option>
        </select></td></tr>
        <tr><td><button id="addToCart" name="addToCart">Add to Cart</button></td></tr>

</table>
</form>

I need to simply create a shopping cart that will display all of the added items and total price when viewed by the user. This wouldn't be too hard, if it was a single value, but since I need to send/store 3 values; Colour, Design and Pop Colour, I am not sure how i should do it.

I have tried:

> Sending the selected data to a table in a database. And then getting the whole table when viewing the Cart
> Using javascript to Find the ID of a shirt depending of what has been selected (Failed)

So my QUESTION is:

How would i create a shopping cart for something like this in the simplest manner. Or how do i send multiple values accross and group them so i can create a cart.

Thank you Very Much!!

Steps to create shopping cart:

1) Have an object to store the values eg. product

product = { 
// product properties

color: "color", 
design: "skull", 
popColor: "color"
}

2) On submit of the form, in cart_main.php fetch the values and populate in the product object

3) You need to have a cart array.

4) To that cart array, add the object. So now you have list of objects inside the cart array.

In the UI you just need to show the count of objects in the array.

When they cart is clicked, you need to iterate the objects and populate the properties