After Refreshing The Page Product Adding Automatically In Cart
Solution 1:
i found this code:
$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1;
this code always run when u reload the page, give some condition if you want to add manually
Solution 2:
If you simply click refresh on the exact same page then action=addcart
etc. is still in the URL. Therefore inevitably it runs that action again when the page loads, and adds the items to the cart again.
An "add" action like that would be better done as a POST request, partly for semantic reasons (it's "sending" data rather than "get"ting it) and partly to avoid annoyances like this. Ideally a GET request should not cause any change of state in the application.
Instead of
<ahref="my-cart?action=addcart&p_id=<?phpecho$p_user_id;?>">Add to cart</a>
you can do something like:
<formaction="my-cart"method="POST"><buttontype="submit">Add to cart</button><inputtype="hidden"name="action"value="addcart"/><inputtype="hidden"name="p_id"value="<?phpecho$p_user_id;?>"/></form>
You can use some CSS to make the button appear more like your old hyperlink, if you wish.
Then, in your PHP cart code, simply replace all references to $_GET
with $_POST
instead, to read the values from the POST request.
This will have the advantage that the variables are not saved into the URL and therefore if someone tries to refresh the URL it will not automatically repeat the same action based on those variables.
You could also look into sending the data to add cart items via AJAX, so that it doesn't require a postback at all and usually results in a smoother user experience - if you look at most shopping websites now, that is generally what they do.
Post a Comment for "After Refreshing The Page Product Adding Automatically In Cart"