立即更新购物车数量,而无需额外的更新购物车按钮。 PHP购物车

时间:2019-05-05 01:23:18

标签: php sql ajax

我曾听说ajax可以在不刷新页面的情况下编辑服务器数据。所以我想知道是否有可能使用ajax只需按一下按钮即可增加购物车数量。 如果更改数量并检出数量成为您写入的新数量,但是如果我更改数量并离开购物车,则数量将重置为将产品添加到购物车时写入的值。可以使用ajax更新购物车会话数组中的存储号码吗?

这是代码的php部分

<?php
      if(!isset($_SESSION["userId"])){
        echo "<div class='loginCart'><p>Du er ikke logget inn</p>";
        include "login.php";
        echo "</div>";
        goto end;
      }
      if (isset($_POST["addToCart"])){
          if (isset($_SESSION["cart"])){
              $product_array_id = array_column($_SESSION["cart"],"productId");
              if (!in_array($_GET["id"],$product_array_id)){
                  $count = count($_SESSION["cart"]);
                  $product_array = array(
                      'productId' => $_GET["id"],
                      'productName' => $_POST["hiddenName"],
                      'productPrice' => $_POST["hiddenPrice"],
                      'productQuantity' => $_POST["productQuantity"],
                  );
                  $_SESSION["cart"][$count] = $product_array;
                  echo '<script>window.location="cart.php"</script>';
              }else{

                  echo '<script>window.location="cart.php"</script>';
              }
          }else{
              $product_array = array(
                  'productId' => $_GET["id"],
                  'productName' => $_POST["hiddenName"],
                  'productPrice' => $_POST["hiddenPrice"],
                  'productQuantity' => $_POST["productQuantity"],
              );
              $_SESSION["cart"][0] = $product_array;
          }
      }
      if (isset($_GET["action"])){
          if ($_GET["action"] == "remove"){
              foreach ($_SESSION["cart"] as $keys => $value){
                  if ($value["productId"] == $_GET["id"]){
                      unset($_SESSION["cart"][$keys]);
                      echo '<script>window.location="cart.php"</script>';
                  }
              }
          }
      }
     ?>
<?php

              if(!empty($_SESSION["cart"])){
                  $totalPrice = 0;
                  foreach ($_SESSION["cart"] as $key => $value) {

            ?>

这是html申请表格

<table class='productTable' style=" margin-top: ; width: 100%; background-color:white; margin:auto; border: solid 2px black;">
            <tr>
              <th width="30%">Produkt navn</th>
              <th width="10%">Antall</th>
              <th width="13%">Pris per</th>
              <th width="10%">Total Pris</th>
              <th width="17%">Fjern produkt</th>
            </tr>


              <tr>
                <td align="center"><?php echo $value["productName"]; ?></td>
                <td align="center">
                  <form method="POST" class="checkoutForm">
                    <input type="number" name="quantity" value="<?php echo $value["productQuantity"];?>" min="0" style="text-align:center; margin: auto; width: 25%;"></td>
                <td align="center"><?php echo number_format($value["productPrice"]);?>-,NOK</td>
                <td align="center"><?php echo number_format($value["productQuantity"] * $value["productPrice"], 2);?>-,NOK</td>
                <td align="center"><a href='cart.php?action=remove&id=<?php echo $value["productId"]; ?>'>Fjern</a></td>
              </tr>

            <?php
            $totalPrice = $totalPrice + ($value["productQuantity"] * $value["productPrice"]);
        }
            ?>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td align="center"><p style="margin: 0;">Totalpris:</p></td>
                <td align="center"><strong><?php echo number_format($totalPrice, 2)."-,NOK"; ?></strong></td>
              </tr>

          </table>
            <input class="CheckOut" type="submit" name="CheckOut" value="Check Out">
          </form>

0 个答案:

没有答案