HTML Input variable overwrite the $_SESSION variable PHP

核能气质少年 提交于 2020-05-24 03:57:32

问题


When I press the add to cart button at my product-page the $_SESSION variable --> $_SESSION['shopping-cart']['quantity'] is set in the the input value. It will display 4 quantity's for example. However, if I manually want to decrease this quantity from 4 -> 5 in the shopping-cart page. It won't remember it's changing and will be overwritten by his original $_SESSION['shopping-cart']['quantity'].

My question is: How can this $_SESSION variable remembers his new input and don't get overwritten by the original $_SESSION variable after refreshing the page.

I hope you understand my question.

 <td data-th="Price"><input type="number" class="form-control text-center" value="<?php echo $product['quantity']; ?>"> </td>

Shopping cart file code: (The session is already started).

    <!-- Shopping Cart Section -->

    <div class="container">
            <table id="cart" class="table table-hover table-condensed">
                <thead>
                    <tr>
                        <th style="width:50%">Product</th>
                        <th style="width:10%">Price</th>
                        <th style="width:8%">Quantity</th>
                        <th style="width:22%" class="text-center">Subtotal</th>
                        <th style="width:10%"></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <?php 
                        if(!empty($_SESSION['shopping-cart'])):
                            $total_payment = 0;
                            foreach($_SESSION['shopping-cart'] as $key => $product):
                        ?>
                        <td data-th="Product">
                            <div class="row">
                                <!--<div class="col-sm-2 hidden-xs"><img src="http://placehold.it/100x100" alt="..." class="img-responsive"/></div>-->
                                <div class="col-sm-10">
                                    <h6 class="nomargin"><?php echo $product['product-name']; ?></h6>
                                </div>
                            </div>
                        </td>
                        <td data-th="Price"><?php echo $product['product-price']; ?></td>
                        <td data-th="Price">

                        <input type="number" class="form-control text-center" value="<?php echo $product['quantity']; ?>">
                        </td>
                        <td data-th="Subtotal" class="text-center"><?php echo number_format($product['quantity'] * $product['product-price'], 2); ?></td>

                        <td class="actions" data-th="">
                            <a href="products/shopping-cart.php"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i></button></a>
                            <a href="includes/shopping-cart.inc.php?action=delete&id=<?php echo $product['id'];?>"><button class="btn btn-danger btn-sm" name="delete-product"><i class="fa fa-trash-o"></i></button></a>                               
                        </td>
                    </tr>
                    <?php 
                    $total_payment += ($product['quantity'] * $product['product-price']);
                    endforeach;
                    endif; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td><a href="categorie/categorie.php" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a></td>
                        <td colspan="2" class="hidden-xs"></td>                         
                        <?php if(isset($_SESSION['shopping-cart'])){
                            if(count($_SESSION['shopping-cart']) > 0){
                                ?>
                                <td class="hidden-xs text-center"><strong>Total price: <?php echo number_format($total_payment, 2);?></strong></td>
                                <td><a href="#" class="btn btn-success btn-block">Checkout <i class="fa fa-angle-right"></i></a></td>
                         <?php   }
                        } ?>



                    </tr>

                </tfoot>


            </table>
        </div>



    <!-- /Shopping Cart Section -->

</main>
<!-- /Main -->


<!-- Footer -->
<?php
include('../structure/footer.php');
?>
<!-- /Footer -->

<!-- Script: Bootstrap CDN, JS -->
<?php
include('../structure/script.php');
?>

Shopping-cart.inc.php file

<?php
$products_in_cart = array();
require('../php/connection.php');
require('../function.php');
//session_destroy();
protect_user();

// Check if Add to Cart button has been submitted
if(isset($_POST['add_to_cart'])){
    if(isset($_SESSION['shopping-cart'])){

        // Keeps track of how many products in shopping cart
        $count = count($_SESSION['shopping-cart']);

        // Create array for matching arrays keys to product id's
        $products_in_cart = array_column($_SESSION['shopping-cart'], 'id');
        if(!in_array(filter_input(INPUT_GET, 'id'), $products_in_cart)){
                $_SESSION['shopping-cart'][$count] = array
                (
                    'id' => filter_input(INPUT_GET, 'id'),
                    'product-name' => $_POST['product-name'],
                    'product-price' => $_POST['product-price'],
                    'quantity' => $_POST['quantity']           
                );
        }
        else{
            for($i = 0; $i < count($products_in_cart); $i++){
                if($products_in_cart[$i] == filter_input(INPUT_GET, 'id')){
                    // Adding item quantity to the existing product in the array
                    $_SESSION['shopping-cart'][$i]['quantity'] += filter_input(INPUT_POST, 'quantity');
                }
            }
        }
    }
    else{
        $_SESSION['shopping-cart'][0] = array
        (
            'id' => filter_input(INPUT_GET, 'id'),
            'product-name' => $_POST['product-name'],
            'product-price' => $_POST['product-price'],
            'quantity' => $_POST['quantity']           
        );
    }
    header("Location: ../products/shopping-cart.php");  
    exit();
}


// Check if Delete product in shopping-cart button has been submitted
if(filter_input(INPUT_GET, 'action') == 'delete'){
    // Loop through all products until matches the deleted product

    foreach($_SESSION['shopping-cart'] as $key => $product){
        if($product['id'] == filter_input(INPUT_GET, 'id')){
            unset($_SESSION['shopping-cart'][$key]);
        }
    }
    $_SESSION['shopping-cart'] = array_values($_SESSION['shopping-cart']);
    header("Location: ../products/shopping-cart.php");  
    exit();
}

header("Location: ../index.php");  
    exit();

Quantity after submitting product

adding extra quantity

来源:https://stackoverflow.com/questions/61878646/html-input-variable-overwrite-the-session-variable-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!