-2

我希望有人可以帮助我解决我遇到的一些麻烦。目前我正在尝试制作一个简单的网上商店。首先一些信息:

我有2种产品,产品a和产品b。这两款产品都有 18 种颜色和 3 种尺寸可供选择。当您想购买产品a时,您只需进入产品a的页面,选择您的颜色和尺码,然后将其添加到您的购物篮中。

我遇到的问题是我无法在会话中存储颜色和大小。现在我将产品的 id 和数量存储在这样的会话中:

 if (isset($_GET['add'])) {
      $_SESSION['cart_'.(int)$_GET['add']]+='1'; 
 }

会话的结果是:cart_1 1。这意味着ID为1的产品,1已被添加到购物篮中。但我想做的是我的会话也可以告诉我提交了哪种颜色和尺寸。($_POST['colour'] 和 $_POST['size']。问题是当我这样存储它时,我只能订购产品 a 的 1 和产品 b 的 1,因为每当我想添加来自cart_1 的产品a 的另一个产品将与具有不同颜色的产品相同。

提前致谢!

4

1 回答 1

1

Assuming that you got all options wrapped in a proper form then you can give each input an id.

Once you're on the current page which handles the form then you can easily access the inputs by referring to their ids.

Example:

<form id="mainForm" method="POST" action="scriptUrl.php">
    <input type="text" id="color" value="" />
    <input type="text" id="size" value="" />
    <input type="submit" id="submitFormButton" value="Submit" />
</form>

Then you can easily access the values as such (once form submitted):

$color = isset($_GET['color']) ? $_GET['color'] : "";
$size = isset($_GET['size']) ? $_GET['size'] : "";

using the ternary operator.

And you can add this to your sessions if you want, it's preferred to have a single session for a single value, though. That is, if you feel that you really have to use sessions; there are other options, sometimes more suitable.

I know you're not using input texts but you can easily modify your example as such.

It differs between people whether they use GET or POST, I prefer POST.

于 2013-11-06T23:56:26.930 回答