javascript - 将 PHP 与 Javascript 结合使用以从购物车中删除商品

标签 javascript php

我正在为我姐姐建立一个网上商店,当我点击产品的 X 图标 (onclick="").

<?php
  if (empty($_SESSION['cart'])) {
      $_SESSION['cart'] = array();
  }
?>

<div class="cart-content d-flex">

<!-- Cart List Area -->
<div class="cart-list">

    <?php
    $subtotal = 0;
    $livrare = 17;
    $subtotal_modif = 0 . " Lei";
    $object = new Produs();

    $cartItems = $_SESSION['cart'];

    foreach ($cartItems as $item):
    $rows = $object->getRows("SELECT * FROM produs");

    foreach ($rows as $row) {
        //$subtotal += $row['pret_produs'];

    if ($item['id'] == $row['id_produs']) {
        $imagini = $object->getRows("SELECT * FROM imagini WHERE id_produs_imagine = ? LIMIT 1", [$row['id_produs']]);

        $pret = $row['pret_produs'];
        $pret_modif = str_replace('.', ',', $row['pret_produs']) . " LEI";
        $pret_vechi = $row['pret_vechi_produs'];
        $pret_redus_modif = str_replace('.', ',', $row['pret_vechi_produs']) . " LEI";

        $subtotal = $subtotal + ($pret * $item['cantitate']);
        $subtotal_modif = str_replace('.', ',', $subtotal) . " LEI";
    ?>


    <!-- Single Cart Item -->
    <div class="single-cart-item">
        <a href="#" class="product-image">
            <?php foreach ($imagini as $img) {

            echo '<img src="'. $object->photoPath() . $img['nume_imagine'] .'" alt="">';
            } ?>
            <!-- Cart Item Desc -->
            **<div class="cart-item-desc">
                <span class="product-remove"><i onclick="removeItem('<?php $item['id']; ?>')" class="fa fa-close" aria-hidden="true"></i></span>**

                <!-- <span class="badge">Mango</span> -->

                <h6><?php echo $row['nume_produs']; ?></h6>
                <p class="size">Marime: <?php echo $item['marime']; ?></p>
                <p class="color">Cantitate: <?php echo $item['cantitate']; ?></p>
                <p class="price"><?php echo $pret; ?></p>
            </div>
        </a>
    </div>
    <?php } }
    endforeach;
    ?>

</div>

我正在考虑在页面末尾做这样的事情,但我不知道如何正确地做:

<script>
    function removeItem(itemID) {
        <?php unset($_SESSION['cart']['<script>itemID</script>']); ?>
    }
</script>

我不知道如何结合使用 PHP 和 JavaScript。

最佳答案

您可以将其放在 PHP 脚本的顶部:

if ( empty( $_SESSION['cart'] ) ) {
    $_SESSION['cart'] = [];
}

if ( isset( $_POST['remove_item'] ) ) {
    $itemID = $_POST['remove_item'];
    if ( isset( $_SESSION['cart'][ $itemID ] ) ) {
        unset( $_SESSION['cart'][ $itemID ] );
    }

    echo $itemID;
    die();
}

// THE REST OF YOUR PHP CODE.

根据项目的 id 给项目的容器一个唯一的 id:

<div class="single-cart-item" id="single-cart-item-<?php echo $item['id']; ?>">
    <!-- --------------- -->
</div>

这在你的 JS 中:

<script type="text/javascript">

    function removeItem( itemID ) {

        // make AJAX request to server to remove item from session.
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "cart.php", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("remove_item=" + itemID);
        xhttp.onreadystatechange = function() {
            if (this.readyState === 4 && this.status === 200) {
                var element = document.getElementById("single-cart-item-" + this.responseText);
                if (element !== null) {
                    element.remove();
                }
            }
        };



    }

</script>

函数 removeItem( itemID ) 正在对您的 PHP 脚本进行 AJAX 调用。它将项目 ID 作为 POST 值传递。将 cart.php 替换为正确的路径(您的购物车页面的 URL)。

关于javascript - 将 PHP 与 Javascript 结合使用以从购物车中删除商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51580579/

相关文章:

php - 使用 PHP 将带有冒号字符串的 24 小时时钟转换为分钟?

php - 需要一个正则表达式来匹配特定的句子格式

php - 我怎样才能使这个查询防注入(inject)? (PHP)

javascript - 从 HTTP 请求 header 中获取 cookie

javascript - Angular - 将工厂变量复制到私有(private)变量中

c# - 如何通过javascript防止回发

javascript - Jest 模拟未覆盖函数或返回错误

php - 从 3 个表中选择数据时,从 2 列中选择非空列

php - PHP和MySQL:mysqli_num_rows()期望参数1为mysqli_result,给定 bool 值

javascript - 使用验证插件 jquery 验证输入数组 - 显示每个输入的错误