On which page put the JS script

0

I have a script to increase the amount of each product in the cart via javascript, but I do not know which page to put it in.

 <script> 
     document.getElementById("diminuir").onclick = function(){myFunc1()};
    document.getElementById("aumentar").onclick = function(){myFunc2()};

     function myFunc1() {
    if ($_SESSION['itens'][$idproduto] > 0) {
      var newVal = parseFloat($_SESSION['itens'][$idproduto]) - 1;
    } else {
      newVal = 0;
    }
}
     function myFunc2() {
   var newVal = parseFloat($_SESSION['itens'][$idproduto]) + 1;
}
      $_SESSION['itens'][$idproduto]= newVal;     
                </script>

I have a page where the customer chooses the products he wants to add to the cart through this code:

<a class='fa fa-cart-plus' style='font-size:30px' href='cart.php?add=carrinho&id=".$row['id_mol_comp']."'></a> </div>

this reference to the cart.php page that contains only php:

 <script> 
     document.getElementById("diminuir").onclick = function(){myFunc1()};
    document.getElementById("aumentar").onclick = function(){myFunc2()};

     function myFunc1() {
    if ($_SESSION['itens'][$idproduto] > 0) {
      var newVal = parseFloat($_SESSION['itens'][$idproduto]) - 1;
    } else {
      newVal = 0;
    }
}
     function myFunc2() {
   var newVal = parseFloat($_SESSION['itens'][$idproduto]) + 1;
}
      $_SESSION['itens'][$idproduto]= newVal;     
                </script>
<?php 
session_start(); 
if (!isset($_SESSION['itens'])){ 
$_SESSION['itens']=array(); 
} 
if(isset($_GET['add'])&& $_GET['add']=="carrinho") { 
$idproduto = $_GET['id']; 
if(!isset($_SESSION['itens'][$idproduto])){ 
$_SESSION['itens'][$idproduto]=1; 
}else{ 
$_SESSION['itens'][$idproduto]+=1; 
} 
} 
header('location: carrinho.php'); 
?>

Finally, there is a final page cart.php to print the cart with the prudutos.

My question is which of the pages to put the script to make the buttons increase and decrease the amount

    
asked by anonymous 12.06.2018 / 13:40

0 answers