1

I have a function that I call with ?clear-cart appended to the end of my page; as you can probably guess, this function clears the user's cart.

I call it like this

<a href="?clear-cart">Clear Cart</a>

Which works great (in that it loads the same page, but now the cart is cleared), except that the URL in the address bar now reads

http://test.local/cart?clear-cart

Is there anyway to call ?clear-cart but have the URL return without the parameter? (Hide it from the user, since I'm only using it for an internal function call??)

6 Answers 6

3

You could clear the cart and then immediately redirect using header (obviously before any output!).

<?php
    header('Location: http://test.local/cart');
    ... clear the cart ...
?>

See the PHP reference manual for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

@neezer: don't forget die() after header.
In this case, I think exit() would be more appropriate.
3

Instead of a GET-request use a POST-request?

2 Comments

I understood I couldn't use POST requests with <a> hyperlinks.
@neezer, with a little javascript in the onclick event method, anything is possible.
1

Something along the lines of this should do what you want:

if (isset($_GET['clear-cart'])) {

  clear_cart();
  header('Location: http://test.local/');

}

Modify to suit your needs.

Comments

0

You can write your cart clearing code in another script and have it redirect back to your page when it's done.

Comments

0

Well, in the function that clears the cart, you could redirect the user to the correct URL, using the header() function.

Comments

0

Have the page http://test.local/cart link to the page http://test.local/cart?clear-cart, which then redirects the user back to http://test.local/cart.

/cart -> /cart?clear-cart -> /cart

if (isset($_GET['clear-cart'])) {
  // Do some cart clearing...
  // Redirect back
  header('Location: /cart');
  exit;   // Very important, otherwise the script will continue until it finally redirects
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.