php删除按钮不断检索发布的信息

发布于 2024-08-17 01:38:20 字数 451 浏览 9 评论 0原文

我有一个很快的问题,我有一个购物车的删除按钮,除了购物车中的最后一个产品之外,代码每次都适用。如果用户按下 addtoCart 按钮,URL 将重新加载 ?buyproduct=$productNumber,并且当按下删除按钮时,产品将被删除。是的,所以一切都很好,但是当您尝试删除最后一项时,它会继续读取 URL 中的产品。因此当前 $productNumber 的数量仍为 1。

我尝试在表单方法标记中添加操作,以便页面在没有 ?buyproduct=$productNumber 的情况下重新加载,这确实有效,但是 URL 中也有页码和部分,这些也重置。

我知道删除正在起作用,因为一旦 ?buyproduct=$productNumber 从 URL 中消失(例如,如果它们转到目录中的另一个部分,就会发生这种情况),然后购物车就可以完全清空。

I have a quick problem, I have a remove button for a shopping cart and the code works for it everytime except for the last product thats in the cart. If a person pushes the addtoCart button the URL is reloaded with ?buyproduct=$productNumber and when the remove button is pushed the product is removed. Right so everything is good, but when you try to remove the last item, it keeps reading the product thats in the URL. so the quantity remains 1 for the current $productNumber.

I've tried adding the action in the form method tag so that the page reloads without the ?buyproduct=$productNumber, which does work however there are page numbers and sections that were also in the URL and these get reset also.

I know the remove is working because once the ?buyproduct=$productNumber is gone from the URL (which can happen for example if they go to another section in the catalog) then the cart can be completely emptied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-08-24 01:38:20

您最好调查一下现有的操作,而不是更改 表单操作做。看起来这就像设置或取消设置某些 GET 变量一样简单。

您可以从表单操作中指定的脚本中发布一些代码吗?

Instead of changing the form action, it seems prudent for you to investigate what the existing action does. It seems as if this could be as simple as setting or unsetting some GET variable.

Can you post some code from the script named in your form's action?

太阳公公是暖光 2024-08-24 01:38:20

显然,您的产品删除表单提交被您的 FORM 操作也试图将产品重新添加到您的购物车这一事实所抵消。也许最简单的解决方案是向 FORM 操作添加一个开关,例如 ?remove=1,然后您可以在处理 buyproduct 的情况之前检查该开关,完全跳过该部分。

但请注意,这不是最干净的解决方案,因为您将不必要的 GET 变量发送到服务器(主要是 buyproduct)。解决此问题的方法可能是您只需为表单操作重新生成查询字符串:

// allowed keys is used to sanitize GET data by only allowing a predefined
// set of keys to be submitted with the form
$allowed_keys = array('page' => true, 'limit' => true, 'othervar' => true);
$str = 'path_to_form_action.php?';
foreach ($_GET as $k => $v) {
    if (isset($allowed_keys[$k])) {
        $str .= $k . '=' . $v . '&';
    }
}
$str = substr($str, 0, -1);

然后您需要使用 $str 作为您的 FORM 操作:

<form action="<?php echo $str; ?>" method="GET">

Clearly your product removal form submission is being offset by the fact your FORM action is also trying to re-add the product to your cart. Perhaps your easiest solution would be to add a switch to your FORM action like ?remove=1 which you can then check for before handling the case of buyproduct, skipping that section entirely.

Note that this is not the cleanest solution, however, because you have uneccessary GET variables being sent to the server (mainly buyproduct). A workaround for this could be for you to simply regenerate the query string for your form action:

// allowed keys is used to sanitize GET data by only allowing a predefined
// set of keys to be submitted with the form
$allowed_keys = array('page' => true, 'limit' => true, 'othervar' => true);
$str = 'path_to_form_action.php?';
foreach ($_GET as $k => $v) {
    if (isset($allowed_keys[$k])) {
        $str .= $k . '=' . $v . '&';
    }
}
$str = substr($str, 0, -1);

You would then want to use $str as your FORM action:

<form action="<?php echo $str; ?>" method="GET">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文