samedi 1 août 2015

How to push data to array using ajax

I have a page listing products - each having an add to cart button. Using the data attribute in the button, I need push the offerId string into an existing array. The expected result after adding multiple products to the cart would be to return an array with all the offerIds. Currently, if I print_r the offerIdArray, it only shows the latest button's offerId.

Thoughts?

$(document).ready(function () {               

    $(".addtocart button").click(function () {

        var offerIdArray = []; 
        var offerId = $(this).attr("data-offerId");                                         

        $.post("controller/create-cart.php",
            {                                
                offerIdArray: offerIdArray.push(offerId)
            },
        function (data) {
            $('body').append(data);
        });                  
    });
});

Aucun commentaire:

Enregistrer un commentaire