Skip to content
Advertisement

How to increase number in variable each time a button is clicked in php

I am creating a shopping cart which I have created using php. I have two buttons that when clicked allow the user to increase or decrease the quantity of the item.

When the user clicks the plus button the quantity goes up by one, however if they click it again nothing happens. is there a way that I can allow the user to say click the button until the quantity goes up to 20?

Here is my code:

This first part of my code checks to see if the original add to cart button has been clicked on an item. If it is it then gets the ID of the item. Once the item ID has been revived it them checks if a cart session has been created. If no shopping cart session has been found then it will create one. Once the session has been create it will then check for the quantity that has been add to the form. Next it creates an array call item which will hold the ID and quantity for the item.

if(isset($_POST['add_to_cart']) && isset($_GET['id'])) {
    $id = $_GET['id'];

    // Check if there is a session for the cart set already, if not then set one.
    if(!isset($_SESSION['shoppingcart'])) {
        $_SESSION['shoppingcart'] = [];
    }

    $quantity = $_POST["amount"];

    $item = [
        "id"        => $id,
        "quantity"  => $quantity
    ];
    }

Next on the cart file the code checks if there is a cart session and that there is at least one item. If there is at least one item in the cart then the code will one a for each loop to get each item from the database.

if(isset($_SESSION['shoppingcart']) && count($_SESSION['shoppingcart']) != 0) {
    $list = $_SESSION['shoppingcart'];
$total = 0;
    foreach($list as $item) {
        $id = $item['id'];

        $query = "SELECT * FROM item WHERE ItemID = '$id'";

        $result = mysqli_query($mysqli, $query);
        
    $row = $query_result->fetch_assoc();

        if (mysqli_num_rows($result) == 1){
            $row = mysqli_fetch_array($result);

            $id = $row['ItemID'];
            $name = $row['ItemName'];
            $image = $row['ItemImage'];
            $price = $row['Price'];
            $size = $row['Size'];
            $quantity = $item['quantity'];

            
        $subtotal = $price*$quantity;
        $total += $subtotal;
            
            ?>

HTML:

<form action="#" method="post">
  <h4>Quantity:  </h4>
  <input type="submit" value="+" name="plus" id="plus" /> 
  <input type="text" class="form-control" id="quanitiy" name="quantity" value= <?php echo "$quantity"; ?>>
  <button type="submit" name="minus"><i class="bi bi-dash"></i></button>
</form>

PHP:

<?php
if(isset($_POST['plus']) && ($quantity < 20)){
    $_SESSION['quantity'] = $quantity++; 
} ?>

I know that this code is in victim of sql injections, I will be fixing it once I get this part working.

Advertisement

Answer

$quantity doesn’t seem to actually be defined anywhere in your code. It’s a bit unclear how even the first increment works, based on what you’ve shown. Perhaps you omitted some detail from the question?

Based on the information available though, this would seem to be more logical:

<?php
session_start();

if(isset($_POST['plus'])) {
   $quantity = (isset($_SESSION['quantity']) ? $_SESSION['quantity'] : 0);

   if ($quantity < 20) {
     $quantity++; 
     $_SESSION['quantity'] = $quantity; 
   }
}

This will check for an existing value, and use that as the starting point to increment from. If there’s no existing value it will start from 0.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement