简体   繁体   English

如何在不刷新页面的情况下创建购物车以在其中添加多个项目

[英]How to create a shopping cart to add multiple items in it without page refresh

I am building an online shop website model for a CS50 project and I am currently having a trouble with creating a shopping cart.我正在为 CS50 项目构建在线商店网站模型,但目前在创建购物车时遇到问题。

I can build a very simple model of a cart where I'd redirect a user to some add_to_cart() function on a button click for all the "Buy" buttons on the page.我可以构建一个非常简单的购物车模型,我可以通过单击页面上所有“购买”按钮的按钮将用户重定向到某个 add_to_cart() 函数。

What I am trying to achieve is to do it without redirecting to another page on every click.我想要实现的是在每次点击时都不会重定向到另一个页面。 For example, I've seen online shops with many items and their according "Buy" buttons on a page, and on a button click I simply see a counter on Cart element increasing, and a user can continue shopping on the page and only go to the Cart page to finalize the purchase when they are done.例如,我看到网上商店有很多商品,页面上有相应的“购买”按钮,点击按钮时,我只会看到购物车元素上的计数器增加,用户可以继续在页面上购物,然后只去到购物车页面以在完成后完成购买。

I have this block of a template code that I am working with atm trying to solve the required Python, and probably some JS code to go with it:我有一个模板代码块,我正在使用 atm 尝试解决所需的 Python,可能还有一些 JS 代码与之配套:

<form action="/catalog" method="post">
{% for item in goods %}
            <li class="list-group-item list-item-no-leftpad">
                <div class="item-info-description">
                    <p><a href="/item?id={{ item.item_id }}">{{ item.brand }} {{ item.model }}</a>
                    <p>{{ item.short_description }}
                </div>
                <div class="item-info-offers">
                    <p>Price: ${{ item.price }}
                    <p>In stock: {{ item.in_stock }}
                    <p><button type="submit" class="btn btn-primary" name="buy" value="{{ item.item_id }}">Buy</button>                            
                </div>
            </li>
{% endfor %}
</form>

The issue is that every time a user tries to update the item, the browser is making a POST request to your web server (or application).问题是每次用户尝试更新项目时,浏览器都会向您的 Web 服务器(或应用程序)发出POST请求。 This involves POST -ing the form data and re-generating the whole page and displaying the updated contents to the user.这涉及POST表单数据并重新生成整个页面并向用户显示更新的内容。 This is handled in the <form action="/catalog" method="post">这是在<form action="/catalog" method="post">

What you are looking for is Client side code to update store and update information based on interaction from the user.您正在寻找的是客户端代码,用于根据用户的交互更新存储和更新信息。 The tried and tested method is to use cookies (to store information) and JavaScript (to modify it).久经考验的方法是使用 cookie(存储信息)和 JavaScript(修改它)。 The simplest way to store the cart info (or rather organize it) is by to use JSON (which is just a string in the end) and store it in the cookie.存储购物车信息(或更确切地说是组织它)的最简单方法是使用JSON (最后只是一个字符串)并将其存储在 cookie 中。

So what you need to do is:所以你需要做的是:

  1. Do not POST every time a user tries to update the cart.不要POST每一个用户试图更新时间车。 To do that;要做到这一点;
  2. In your client-side code, add an event listener for the button click for all the items that you display.在您的客户端代码中,为您显示的所有项目的按钮单击添加一个事件侦听器。
  3. Add a different button that POST 's any additional info (Cookies are sent automatically by the browser)添加一个不同的按钮,用于POST的任何附加信息(浏览器自动发送 Cookie)
  4. Read the cookie server-side and generate a checkout page (or any other result based on your business logic)读取 cookie 服务器端并生成结帐页面(或基于您的业务逻辑的任何其他结果)

Here are a few references to get you started.以下是一些帮助您入门的参考资料。

UPDATE: You might want to use local storage API instead, in order to better comply with GDPR.更新:您可能希望改用本地存储 API,以便更好地遵守 GDPR。

JS - Cookies JS - 饼干
JS - React to button clicks JS - 对按钮点击做出反应
JS - Get clicked object JS - 获取被点击的对象

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

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