SHARE
TWEET

add-to-cart

a guest Oct 22nd, 2019 88 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. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top