Advertisement
Guest User

add-to-cart

a guest
Oct 22nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. let addtocart_buttons = document.querySelectorAll('.add-to-cart-btn');
  4.  
  5. addtocart_buttons.forEach((addToCartBtn) => {
  6.     addToCartBtn.addEventListener('click', (button) => {
  7.         let id = button.target.getAttribute('data-id');
  8.         let quantity = button.target.previousElementSibling.value;
  9.         console.log(quantity);
  10.  
  11.         if (quantity < 0) {
  12.             alert("Please enter a quantity");
  13.         } else {
  14.             let data = new FormData;
  15.  
  16.             data.append('id', id);
  17.             data.append('quantity', quantity);
  18.  
  19.             fetch("../../controllers/process-update-cart.php",
  20.                     {
  21.                         method: "POST",
  22.                         body : data
  23.                     })
  24.                 .then((response) => {
  25.                     return response.text();
  26.                 })
  27.                 .then((data_from_fetch) => {
  28.                     console.log(data_from_fetch);
  29.                 });
  30.         }
  31.     });
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement