简体   繁体   English

如何获取更新的购物车 session 数组并将其显示在另一个 php 文件 AJAX 中

[英]How can i get the updated cart session array and display it in another php file AJAX

AJAX AJAX

$(document).ready(function(){
//remove product from cart
                $(".delete-product-cart").click(function(e){

             var id = $(this).data('id');
             $.ajax({
                 url: "remove_from_cart.php",
                 type: "GET", //send it through get method
                 data: {
                     id: id,
                 },
                 success: function(response) {


                 },
                 error: function(xhr) {
                     //Do Something to handle error
                 }
             });

                });
});

//remove_from_cart.php //remove_from_cart.php

<?php
// start session
session_start();

// get the product id
$id = isset($_GET['id']) ? $_GET['id'] : "";


// remove the item from the array
unset($_SESSION['cart'][$id]);
?>

// Then i have the cart.php where i press the button to delete a product in a while loop The session cart inside the FOREACH is what's important to update the product WITHOUT reloading the page i guess. // 然后我有购物车.php,我按下按钮在while循环中删除产品FOREACH内的session购物车对于更新产品很重要,而无需重新加载我猜的页面。 So how will THIS session cart will be updated without cart.php reloading??那么这个 session 购物车将如何在没有购物车的情况下更新。php 重新加载?


if(count($_SESSION['cart'])>0){

    // get the product ids
    $ids = array();
    foreach($_SESSION['cart'] as $id=>$value){
        array_push($ids, $id);
    }

    $stmt=$product->readByIds($ids);


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

product with ID here

    }
}

I think this will help you.我想这会对你有所帮助。 I used as manny of your own code.我使用了很多你自己的代码。 There are many points of improvements but i guess this is a learning project of yours有很多改进点,但我想这是你的一个学习项目

<?php
/**
 * remove_from_cart.php
 */
// Test if the script gets loaded by a POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {

  session_start();
  $id = (int)$_POST['id'];
  // remove the item from the array
  unset($_SESSION['cart'][$id]);

}


/**
 * cart.php
 */
if (isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {

  // get the product ids
  // Wy do this? your session is already an array with the ids
  $ids = array();
  // And here you add the key of the array to ids and not the product_id so you will get the wrong products
  foreach ($_SESSION['cart'] as $id => $value) {
    array_push($ids, $id);
  }

  $stmt = $product->readByIds($_SESSION['cart']); // Instead of $ids you can add the session

  // this works, but add your fetch in your readByIds method to keep your code cleaner and return the array with products
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    ?>
    <div class="product-row">
      <p><?= $row['product_name'];?></p>
      <p><?= $row['product_price'];?></p>
      <button type="button" class="delete-product-cart" data-id="<? $row['product_id']; ?>">Remove</button>
    </div>
    <?php

  }
} else {
  // Cart session doesnt exist or is empty
  // Let the user know the his cart is empty
}
?>

<script>
  $(function () {
    // Remove product from cart
    $(".delete-product-cart").click(function (e) {
      var
        button = $(this),
        product_id = button.data('id');

      $.ajax({
        url: "remove_from_cart.php",
        type: "POST",
        data: {
          id: product_id,
        },
        beforeSend: function() {
          // Disable the button, to prevent duplicate request
          button.attr('disabled', 'disabled');
        },
        success: function (data) { // Response data is not used now
          button.parent().fadeOut(300, function () {
            button.remove();
          });
        },
        complete: function() {
          // Make the button enabled again, for when the delete function didnt work the button can be pressed again
          button.removeAttr('disabled');
        }
        error: function () {
          //Do Something to handle error
        }
      });
    });

  });

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM