简体   繁体   English

在图像切换上更新php Session变量

[英]Updating php Session variable on image toggle

I am trying to allow users to add/remove items from a shopping cart by simply clicking an image. 我试图让用户只需点击图片即可从购物车中添加/删除商品。 I also want the image to change on click to show how many of this item has been purchased. 我还希望图像在点击时更改,以显示已购买此项目的数量。

Finally, I want the cart to exist on a separate tab of the same page. 最后,我希望购物车存在于同一页面的单独选项卡上。

What I have so far is: 到目前为止我所拥有的是:

<?php session_start(); ?>
...
...
<div style="display: block; width:1024px;">
<img id="image_id" src="image.png" width="110" height="110" style="margin-left:40px";>
<script>

$('#image_id').toggle(
    function() {
        $(this).attr('src','image1.png');
        <?php $_SESSION['cart']['image_id'] = 1; ?>
    },
    function() {
        $(this).attr('src','image2.png');
        <?php $_SESSION['cart']['image_id'] = 2; ?>
    },
    function() {
        $(this).attr('src','image3.png');
        <?php $_SESSION['cart']['image_id'] = 3; ?>
    },
    function() {
        $(this).attr('src','image.png');
    <?php// $_SESSION['cart']['image_id'] = 0; ?>
    }
);
</script>
...
...
</div>

<!-- Cart Tab -->

<div id="tab4" style="display:none; width:1024px; padding-top:40px;">

<table border="1">
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>

<?php
    if($_SESSION['cart']) {
        foreach($_SESSION['cart'] as $itemname => $quantity)
        {
            if($quantity == 0) continue;
            $price = $_SESSION['items'][$itemname];
            $prod = $price*$quantity;
            $_SESSION['total'] += $prod;
            echo "<tr>";
            echo "<td>" . $itemname . "</td>";
            echo "<td>" . $price . "</td>";
            echo "<td>" . $quantity . "</td>";
            echo "<td>" . $prod . "</td>";
            echo "</tr>";
        }
        echo "<tr>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td>" . $_SESSION['total'] . "</td>";
        echo "</tr>";
        echo "</table>";
    }    
?>

<a href="#tab1">Continue Shopping</a>

</div>

The problem I'm having is that the image changes correctly (only changes when I click it and toggles through the four different images), but when I view the contents of the cart, it doesn't change. 我遇到的问题是图像正确更改(只有当我点击它并切换四个不同的图像时才会改变),但是当我查看购物车的内容时,它不会改变。 It executes all 4 php statements immediately. 它立即执行所有4个php语句。 I know because if I remove the final toggle switch(where I reset the quantity to 0) and view the cart, it already shows that I have 3 items to be purchased before I even click once. 我知道,因为如果我移除最后一个切换开关(我将数量重置为0)并查看购物车,它已经显示我甚至在点击一次之前有3个要购买的商品。

Any help would be appreciated. 任何帮助,将不胜感激。 I am guessing I could also have constructed the images as form inputs, or hyperlinks that execute a script on click, but I have tried both approaches with no luck. 我猜我也可以构建图像作为表单输入,或者在点击时执行脚本的超链接,但我尝试了两种方法都没有运气。

Thank you in advance. 先感谢您。

Javascript is executed in the browser, and php is executed on the server, before the page is served to the user. Javascript在浏览器中执行,并且在将页面提供给用户之前在服务器上执行php。

Your PHP snippets are not executed when the javascript is called, rather they are executed on the server before the user even sees the page. 调用javascript时不会执行PHP代码段,而是在用户查看页面之前在服务器上执行它们。

Instead of putting the PHP code directly in the javascript functions you need to use an AJAX call there that sends a request to the server. 不是将PHP代码直接放在javascript函数中,而是需要使用AJAX调用向服务器发送请求。

That said, it looks like in this particular case it might be better to store the cart information in cookies rather than the session. 也就是说,在这种特殊情况下,将购物车信息存储在cookie而不是会话中可能会更好。

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

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