0

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

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

我知道删除是有效的,因为一旦?buyproduct=$productNumber从 URL 中删除(例如,如果他们转到目录中的另一个部分,可能会发生这种情况),那么购物车就可以完全清空。

4

2 回答 2

0

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?

于 2010-01-01T14:13:15.790 回答
0

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">
于 2010-01-01T14:43:39.430 回答