Shopping Cart Product Page

Hey guys/gals, just a quick one, I’m developing a shopping cart for my work, when I user a ‘Adds to Basket’ which is a submit button, it refreshes the page, most users will want to go back but if they’ve added a product it says about Resubmitting a Form - I don’t want to confuse customers. What would be a simple workaround? Thanks in advance :smiley:

Post/Redirect/Get :wink:

Hey bud, thanks for the quick reply. Taken a read of that article and done a bit of browsing for it on the web - still a little puzzled though as to how this can be incorporated into a shopping cart for my ‘Add to Basket’ button…any ideas? I’m a bit slow tonight, hectic day! :slight_smile:

Okay, let’s say you have to add a product to a cart. You will have 2 scripts, once called view.php and another add.php.

view.php will list the available products, with a form to submit to add.php. add.php will save the products to the cart/db then once complete, redirect the user back to view.php.

view.php -> POST -> add.php -> REDIRECT -> GET -> view.php


<html>
  <head>
    <title>View Products</title>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
          <th>Add</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Yellow shoes</td>
          <td>1.99</td>
          <td>
            <form action="add.php" method="post">
              <input type="hidden" name="product_id" value="1" />
              <input type="submit" value="add" />
            </form>
          </td>
        </tr>
        <tr>
          <td>Blue shoes</td>
          <td>1.99</td>
          <td>
            <form action="add.php" method="post">
              <input type="hidden" name="product_id" value="2" />
              <input type="submit" value="add" />
            </form>
          </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>


<?php
/*
  Connect to db, save to db, then redirect.
*/
header('Location: http://example.org/view.php', true, 302);
exit;

Hi Anthony, apologies for the delay in returning your message, thanks for the help, it makes a lot of sense, will have a go at this, sounds interesting. Hectic and busy week…well a couple of days of now :slight_smile:

use ajax to add the item to a shopping cart. Over time sending the user to a cart page after they added the item is just bad practice. shoppers always hit the back button from the cart page.

Hey bud, don’t suppose you have a basic example you could post, still trying to get my head around AJAX, the jQuery route seems to be an obvious way forward I’m thinking…

Just ridiculous.