Search code examples
phpmysqlcartshopping-cartproduct-quantity

I have a problem with updating the product quantity


I have a problem in updating the quantity of the product when I press the button 'plus', the quantity is not updated

<i class="fas fa-plus plus " id="<?php echo $product_id; ?>" ></i>  
<input type="text"   value="<?php echo $product_quantity ?>"      id="quantity3<?php echo $product_id ?>"/>  
<i class="fas fa-minus minus" id="<?php echo $product_id; ?>"  ></i>    
    

<script>
$(document).on('click', '.plus', function(){    
       $.ajax({
          method:"POST",
          data:{ 
                 action:"plus" ,   
                 product_id: $(this).attr("id"),    
                 ip:$('#ip1'+$(this).attr("id")).val(),     
                 product_imgsrira:$('#imgsrira1'+$(this).attr("id")).val(),     
                 product_title:$('#title1'+$(this).attr("id")).val(),       
                 product_prix:$('#prix1'+$(this).attr("id")).val(),           
                 product_quantity: $(this).parent().find('#quantity3'+$(this).attr("id")).val(   parseInt($(this).parent().find('#quantity3'+$(this).attr("id")).val()) + 1  ) 
               },      
         success:function()  {  ajaxx();  alert("Item update");  }
 
        });     
});
        
</script>



<?php
if($_POST['action'] == "plus")
    {   
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["product_id"] == $_POST["product_id"])
            {
             $_SESSION["shopping_cart"][$keys] = array  (  'ip' => get_ip(),  'product_id' => $_POST["product_id"],    'product_imgsrira' => $_POST["product_imgsrira"],    'product_title' => $_POST["product_title"],              'product_prix' => $_POST["product_prix"],   'product_quantity' => $_POST["product_quantity"]          );
 
            }
         }
    }      
?>

I have a problem in updating the quantity of the product when I press the button 'plus', the quantity is not updated


Solution

  • I solved the problem I just moved the code from Ajax

    $(this).parent().find('#quantity3'+$(this).attr("id")).val(   parseInt($(this).parent().find('#quantity3'+$(this).attr("id")).val()) + 1  ); 
    

    I added this code in Ajax

    product_quantity: $('#quantity3'+$(this).attr("id")).val(),
    

    And the final code looks like this

    <i class="fas fa-plus plus " id="<?php echo $product_id; ?>" ></i>  
    <input type="text"   value="<?php echo $product_quantity ?>"      id="quantity3<?php echo $product_id ?>"/>  
    <i class="fas fa-minus minus" id="<?php echo $product_id; ?>"  ></i>    
        
    
    <script>
    $(document).on('click', '.plus', function(){
    
    $(this).parent().find('#quantity3'+$(this).attr("id")).val(   parseInt($(this).parent().find('#quantity3'+$(this).attr("id")).val()) + 1  ); 
        
           $.ajax({
              method:"POST",
              data:{ 
                     action:"plus" ,   
                     product_id: $(this).attr("id"),    
                     ip:$('#ip1'+$(this).attr("id")).val(),     
                     product_imgsrira:$('#imgsrira1'+$(this).attr("id")).val(),     
                     product_title:$('#title1'+$(this).attr("id")).val(),       
                     product_prix:$('#prix1'+$(this).attr("id")).val(),           
                     product_quantity: $('#quantity3'+$(this).attr("id")).val(), 
                   },      
             success:function()  {  ajaxx();  alert("Item update");  }
     
            });     
    });
            
    </script>
    
    
    
    <?php
    if($_POST['action'] == "plus")
        {   
            foreach($_SESSION["shopping_cart"] as $keys => $values)
            {
                if($values["product_id"] == $_POST["product_id"])
                {
                 $_SESSION["shopping_cart"][$keys] = array  (  'ip' => get_ip(),  'product_id' => $_POST["product_id"],    'product_imgsrira' => $_POST["product_imgsrira"],    'product_title' => $_POST["product_title"],              'product_prix' => $_POST["product_prix"],   'product_quantity' => $_POST["product_quantity"]          );
     
                }
             }
        }      
    ?>