douxunchen3498 2013-11-06 23:48
浏览 25

无法让我的$ _SESSION正确

I hope someone can help me with some troubles I'm having. Currently I'm trying to make a simple webshop. First some information:

I have 2 kinds of products, product a and product b. Both of the products are available in 18 colours and 3 sizes. When you want to buy product a, you simply go to the page of product a, select your colour and your size, and then you add it to your basket.

The problem I'm having is that I can't manage to store the colour and size in the session as wel. Right now I'm storing the id of the product and the quantity in a session like this:

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

The outcome of the session is: cart_1 1. It means that of the product with the ID 1, 1 has been added to the shopping basket. But what I want to do is that my session also can tell me which colour and size has been submitted. ($_POST['colour'] and $_POST['size']. The problem is that when I store it like this, I will only be able to order 1 of product a and 1 of product b because whenever I want to add another one of product a the 1 from cart_1 will be the same as the one with a different colour.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongyu6276 2013-11-06 23:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探