【问题标题】:how to change add to cart button after adding product to the cart using php使用php将产品添加到购物车后如何更改添加到购物车按钮
【发布时间】:2021-02-03 02:26:24
【问题描述】:

我不知道该怎么做, 用户将特定产品添加到购物车后,我想更改添加到购物车按钮,我的购物车在会话中工作

  <?php require'admin/dist/db.php';
  $sql = " SELECT * FROM products";
  $res = $conn->query($sql);
  if ($res->num_rows > 0) {
  while ($row = $res->fetch_assoc()) {
  $id=$row['id'];
  $_SESSION['product_id']=$id;?>
  <div class="cards ">
    <div class="wrapper">
      <div class="colorProd"></div>
      <div class="imgProd" style="background-image: url(img/product/<?echo $row['img_path'];?>);"></div>
      <div class="infoProd">
        <p class="nombreProd"><?echo $row['product_name'];?></p>
        <p class="extraInfo"></p>
        <div class="actions">
          <div class="preciosGrupo">
            <p class="precio precioOferta">9,999</p>
            <p class="precio precioProd"><?echo $row['product_price'];?>
            </p>
          </div>
          <div class="icono action aFavs" title="Add TO Wishlist">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
              <path d="M47 5c-6.5 0-12.9 4.2-15 10-2.1-5.8-8.5-10-15-10A15 15 0 0 0 2 20c0 13 11 26 30 39 19-13 30-26 30-39A15 15 0 0 0 47 5z">
              </path>
            </svg>
          </div>
           <form action="" method="post">
          <label >
            <button  class="  pointer addtocart " id="<? echo $row['id'];?>" type="submit" >
            </button>
            <div class="icono action alCarrito">
              <!-- alCarrito -->
              <svg class="inCart" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
                <title>Added To Cart !</title>
              <path d="M30 22H12M2 6h6l10 40h32l3.2-9.7"></path>
              <circle cx="20" cy="54" r="4"></circle>
              <circle cx="46" cy="54" r="4"></circle>
              <circle cx="46" cy="22" r="16"></circle>
            <path d="M53 18l-8 9-5-5"></path>
          </svg>
          <svg class="outCart" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
            <title>Add To Cart</title>
          <path d="M2 6h10l10 40h32l8-24H16"></path>
          <circle cx="23" cy="54" r="4"></circle>
          <circle cx="49" cy="54" r="4"></circle>
        </svg>
      </div>
    </label>
    <input type="hidden" name="id" value="<?echo $row['id'];?>">
    <input type="hidden" name="name" value="<?echo $row['product_name'];?>">
    <input type="hidden" name="price" value="<?echo $row['product_price'];?>">
  </form>
</div>
</div>
</div>
</div>
<? }}?>
</div>
<?if(isset($_POST['id']))
{$name = $_POST['name'];
$price = $_POST['price'];
$id = $_POST['id'];
$cart  = array('id' => $id,'name' => $name,'price' => $price);
$_SESSION['cart'][$id]= $cart;
}?>

我想要做的只是告诉 php 已经添加的产品的购物车按钮发生了变化 this 3 cards output from my data base 在用户将产品添加到购物车后,我想显示添加到购物车按钮,如下所示 see the cart svg i wana change it like this after a product added to cart

如果我在 &lt;div class="cards enCarrito"&gt; 中添加此类,我将有此类用于 css enCarrito 然后购物车 svg 像我在第二张图片中上传的那样发生了变化

谢谢!

【问题讨论】:

  • 两个链接都指向同一张图片。这使得理解您想要实现的目标变得更加困难。
  • 查看购物车 svg 旁边的价格,两张图片看起来不同
  • 你确定吗?我的眼睛可能不像以前那样了,但就链接而言,“i.stack.imgur.com/HxsFQ.png”和“i.stack.imgur.com/HxsFQ.png”看起来与我非常相似。

标签: javascript php html jquery


【解决方案1】:

此答案基于大量猜想,并假设页面在您将某些内容添加到购物车后重新加载。

我假设您将在会话中存储您的购物车,如下所示: $_SESSION['cart'][] = $_POST['id'].

现在,在(重新)构建页面时,您可以使用in_array 函数来确定当前正在构建的项目是否已经在购物车中。

while ($row = $res->fetch_assoc()) {
  $id=$row['id'];
  $inCart = (in_array($id, $_SESSION['cart']) ? 'enCarrito' : '';
?>
  <div class="cards <?= $inCart ?>">

假设您希望使用 JQuery 执行此操作,您应该为您的 div 提供一个正确的 id,如 &lt;div id='product_&lt;?= $id ?&gt;'&gt;,然后使用 $('#product' + ID).addClass('enCarrito'); 添加类,如 https://api.jquery.com/addClass/ 中所述

【讨论】:

  • in_array 函数可以完成这项工作,但是在创建会话后我需要刷新页面,因为在第一次尝试时它只会创建会话并且如果我刷新页面则不会运行 in_array 函数然后代码随心所欲!
  • 如果您想在不刷新页面的情况下执行此操作,请查看我的答案的下半部分,@lucifer,我为您留下了一个小的 JQuery sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2012-06-09
相关资源
最近更新 更多