如果SESSION变量更改,则更新DIV

时间:2019-05-03 15:27:10

标签: javascript php arrays ajax session

我正在尝试制作Shop-Widget,而我的目标就是实现所有功能而无需重新加载Page。我有一个SESSION数组,我将所有文章保存在购物车中。但是,当我添加新文章时,总是需要重新加载页面以查看SESSION Array的更改。我正在对我的add_cart.php进行AJAX调用,然后将产品添加到使用POST参数传递的数组中。

我已经尝试了一些带有加载的JQuery函数,但这没有用。我也尝试使用该存储库,但是没有典型的响应,因为我只是操纵SESSION Array ...

在这里我的文件:

add_cart.php:

<?php
    session_start();
    //check if product is already in the cart
    if(!in_array($_POST['postID'], $_SESSION['cart'])){
        array_push($_SESSION['cart'], $_POST['postID']);
        $_SESSION['message'] = 'Product added to cart';
    }
    else{
        $_SESSION['message'] = 'Product already in cart';
    }

    //unset quantity
    unset($_SESSION['qty_array']);
?>

带有AJAX调用的我的文件:

    $('.rs-buttons_cart').each(function() { //My "Add to Cart"-Button
        $(this).click(function() {
            var productId = $(this).data('productid');
            $.ajax({
                url: '<?php echo $parentWidgetURL; ?>add_cart.php',
                type: 'POST',
                data: {
                    postID: productId
                },
                success: function(data) {
                    window.location.href = "index.html"; //This I added to reload the page to see changes => Want to remove that afterwords :)
                }               
            });
        });
    });

我的购物车DIV如下所示(此容器列出了SESSION阵列中的所有产品):

echo "<div id='rs-cart' class='rs-cartpopup'>";             
                //initialize total
                $total = 0;
                if(!empty($_SESSION['cart'])){

                ?>

                <form method="POST" action="<?php echo $parentWidgetURL; ?>save_cart.php">
                    <table class="rs-cart__table">
                        <thead>
                            <th></th>
                            <th>Bild</th>
                            <th>Name</th>
                            <th>Preis</th>
                            <th>Anzahl</th>
                            <th>Zwischensumme</th>
                        </thead>
                        <tbody>

                <?php

                //create array of initail qty which is 1
                $index = 0;

                if(!isset($_SESSION['qty_array'])){
                    $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
                }


                $sqlCartList = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
                $queryCartList = $conn->query($sqlCartList);
                    while($rowCartList = $queryCartList->fetch_assoc()){
                        ?>
                        <tr>
                            <td>
                                <a href="delete_item.php?id=<?php echo $rowCartList['id']; ?>&index=<?php echo $index; ?>" class="btn btn-danger btn-sm"><i class="fas fa-trash-alt"></i></a>
                            </td>
                            <td><img src='<?php echo $parentWidgetURL; ?>/login/uploads/<?php echo $user."/".$rowCartList["productImage"]; ?>' width='100px' height='auto'></img></td>
                            <td><?php echo $rowCartList['nameDE']; ?></td>
                            <td class="rs-cartprice"><?php echo $rowCartList['price']; ?></td>
                            <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
                            <td><input type="number" min="1" class="form-control js-productvalue" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
                            <td class="rs-subtotal"><?php echo number_format($_SESSION['qty_array'][$index]*$rowCartList['price'], 2); ?></td>
                            <?php $total += $_SESSION['qty_array'][$index]*$rowCartList['price']; ?>
                        </tr>
                        <?php
                        $index ++;
                    }
                    ?>
                    <tr>
                        <td colspan="5" align="right"><b></b></td>
                        <td class="rs-total"><b>Total <?php echo number_format($total, 2); ?></b></td>
                    </tr>
                    </tbody>
                    </table>
                    <br>
                    <div class="rs-cart__buttons">
                        <button type="submit" class="rs-save_cart" name="save">Warenkorb aktualisieren</button>
                        <a href="clear_cart.php" class="btn btn-danger"><input type="button" value="Warenkorb leeren"><span class="glyphicon glyphicon-trash"></span></button></a>
                        <a href="#" class="btn btn-success"><button><span class="glyphicon glyphicon-check"></span> Zur Kasse</button></a>
                    </div>
                    </form>
                <?php
                }
                else{
                    ?>
                    <tr>
                        <td colspan="4" class="text-center"><span class="rs-noarticles">Keine Artikel im Warenkorb</span></td>
                    </tr>
                    <?php
                }

            echo"</div>";

如果您有任何疑问,请告诉我:)谢谢!!!

0 个答案:

没有答案