sql - PHP / AJAX Shopping cart not working correctly

117

I'm having some problems with, building a shopping cart.

I cannot set a different quantity for each items in my shop. The quantity should be picked up from an input field, at that is working fine. But if you're setting the quantity to 3 for item 1, and the quantity to 5 for item 2, every items will have the quantity of 5 (the last added).

Also when you're trying to delete one of the items, the quantity now will be set to zero, of every items. It is like, that the quantity of each item is not stored, and I have no idea why this is happening.

The cart I'm using, is PHP/SQL and AJAX.

This is my code so far:

PHP:

AJAX Call:

// Load building cart, when adding items
$(document).on('click', 'input[type=button].add-button', function() {

    // AJAX - Define data to send
    var actiontype = "add";
        productid = $('input[name="address"]').val();
        quantity = $('input[name="qun"]').val();
    var data = "action="+ actiontype + "&id="+ productid + "&quantity="+ quantity;
    $.ajax({
        url: 'buildingcart.php',
        data: data,
        success:function(result) {  
            // Show result
            $('#building-cart .content').html(result);
        }
    });
});

// Load building cart, when adding items
$(document).on('click', '.remove-button', function() {

    // AJAX - Define data to send
    var actiontype = "remove";
        productid = $(this).parent().find('input[name="partner"]').val();

    var data = "action="+ actiontype + "&id="+ productid;
    $.ajax({
        url: 'buildingcart.php',
        data: data,
        success:function(result) {  
            // Show result
            $('#building-cart .content').html(result);
        }
    });
});

// Load building cart, when adding items
$(document).on('click', '.empty-button', function() {

    // AJAX - Define data to send
    var actiontype = "empty";

    var data = "action="+ actiontype;
    $.ajax({
        url: 'buildingcart.php',
        data: data,
        success:function(result) {  
            // Show result
            $('#building-cart .content').html(result);
        }
    });
});

I tried changing

foreach($_SESSION['cart'] as $product_id => $quantity)

to

foreach($_SESSION['cart'] as $product_id)

with no luck.

I also want to have the ability to set a different size for each item, but the same thing happens here. It always takes the quantity/size of the last added item, and update it to the rest of the items, which is not intended.

Sorry for the long codes.. Hope someone can help me :-)

People are also looking for solutions to the problem: php - How to save field form in database with required fields

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.