简体   繁体   English

使用JavaScript实现购物车?

[英]Implementing a shopping cart using Javascript?

I've been playing with the paypal html code and have been working on a shopping cart, I don't want to use a premade cart system. 我一直在使用paypal html代码,并且一直在购物车上工作,我不想使用预制的购物车系统。

So far my approach has been to store the items added to a cart in an array. 到目前为止,我的方法是将添加到购物车中的项目存储在数组中。 I want to store it in a cookie really but don't know how to update the cookie each time a new item is added. 我真的想将其存储在cookie中,但是不知道每次添加新项目时如何更新cookie。

Any general advice about how to go about it would be great. 任何有关如何执行此操作的一般建议都很好。

Typically, you'll need something on the server side (PHP?) to store such information. 通常,您需要在服务器端(PHP?)上存储一些信息。 In PHP you would do session_start() and it would give a session number cookie to the browser, which would identify it to the server from that point forward (until it expires). 在PHP中,您可以执行session_start() ,并将会话号cookie提供给浏览器,然后从浏览器开始将其标识给服务器(直到过期)。 Then you can use $_SESSION[] to store information regarding the cart, your customer, etc. 然后,您可以使用$_SESSION[]存储有关购物车,您的客户等的信息。

If you're not going to use server-side persistence, then you'll have to create lots of messy cookies or pass stuff back and forth in the URL. 如果您不打算使用服务器端的持久性,则必须创建很多凌乱的cookie或在URL中来回传递东西。

Regardless of whether you do pure client-side persistence or server-side , you'll want to check and validate all data once submitted to the server because it can be manipulated by the user before submission. 无论您是纯粹的客户端持久性还是服务器端 ,您都希望在提交给服务器后检查并验证所有数据,因为用户可以在提交之前操纵这些数据。 So prices, discounts can be changed and sent by the user. 因此,价格,折扣可以由用户更改和发送。 Check 'em before accepting them at the server. 在服务器上接受它们之前,请先检查它们。

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

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