views:

57

answers:

2

Hi every1! im quickly finding im quite the beginner to php, so please bare with me!

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.

Thank you so much ahead of time for any1's help, I've used this site before and you guys are genius's!

A: 

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?

jjclarkson
A: 

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">
cballou