gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:20:28 25 4
gpt4 key购买 nike

我正在为我姐姐建立一个网上商店,当我点击产品的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com