• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

PHP self and history

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    PHP self and history

    Fed up with these complicated, umpteen file freeware things that don't work so am trying to write my own simple shopping cart in PHP. I am far from expert in this language. Basic code on a single product page is:

    <?php
    if(isset($_POST['add']))
    {
    blah, blah - add cart item to database
    }
    ?>

    blah, blah HTML

    <form name="checkout" method="post" action="" action="">
    <button type="submit" name="add" value="add"/>Add to cart</button>

    blah, blah more HTML

    All works great but thought it was too easy. Found problem with self calls in that the browser history gets longer and longer, so if user adds to cart twice he has to click back three times to return to the main page. Can't delete history in PHP so is there a better way of structuring things? Or do I have to resort to cookies?

    Much ta.
    Last edited by xoggoth; 22 October 2013, 20:14.
    bloggoth

    If everything isn't black and white, I say, 'Why the hell not?'
    John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

    #2
    Originally posted by xoggoth View Post
    Found problem with self calls in that the browser history gets longer and longer, so if user adds to cart twice he has to click back three times to return to the main page.
    That's not a PHP thing - it's an HTTP thing combined with the browser's history implementation. I'd expect anything to work that way. When I submit a form, I'm navigating to a new state. The fact that the form happens to bring me back to the same place shouldn't mean it's ignored by the browser history.

    As an experiment, I turned off JavaScript (to prevent Ajax confusing matters and ensure I was working purely with HTTP and the browser history) and visited Amazon's home page. There I followed a link to some book's product page, then added it to my basket. On the "you added this to your basket" page I added one of the recommended items below that message to my basket. That landed me on the "you added this to your basket" page for the second product.

    Then, using the back button: the first click took me back to the "yattyb" page for the first item;the second click took me back to the product page for the first item; and the third click took me back to the home page.

    All this is exactly as one would expect, and sounds just like how yours is working.

    Comment


      #3
      Cheers again. You're right. Ever look at those things in online newspapers? 10 world's most boring cities or whatever, you scroll through the images and then have to do back 10 times to leave the page.

      Bunged this at end of function, seems to work ok. For few people with no jscript a couple of extra clicks is no big deal anyway esp. if I put a continue shopping button on.

      echo '<script type="text/javascript">window.history.back(); </script>';

      PS Darn things should work how I want them to work, not how they should work!
      bloggoth

      If everything isn't black and white, I say, 'Why the hell not?'
      John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

      Comment


        #4
        This might be why a lot of sites use JavaScript to add items to a basket... Yes, that probably would mean resorting to cookies though!

        Comment


          #5
          If you stick a link on there saying "Back to home page" (or whatever seems suitable) and dress it up nicely, 99 people out of 100 will use that instead of the browser back button

          Comment

          Working...
          X