0

I have a shop which uses dynamic content. Users can click on checkboxes to view products of specific categories. For this I am using jQuery to get products from another php file (works fine).

shop.php:

<input type="checkbox" class="category_btn" name="cat" id="category-books" value="books">
<input type="checkbox" class="category_btn" name="cat" id="category-dvds" value="dvds">
...    
<div id="allProducts">
   Load dynamic content with jQuery here...
</div>

loadProducts.php:

<?php
$categories = $_POST['categories'];
$productsData = load_products($categories);

for ($i = 0; $i < $amount; $i++) {?>
    <div class="singleproduct">
        <a href="product.php?id=<?php echo $productsData[$i]['ID'] ?>"><img
                    src="images/<?php echo get_image($productsData[$i]['ID']) ?>"
                    alt=""></a>
        <p class="prod-title"><?php echo $productsData[$i]['title'] ?></p>
    </div>
<?php }?>

jQuery code (shop.php):

<script>
    jQuery(document).ready(function($) {
        var checkedCats = [];
        $(".category_btn:checked").each(function() {
            checkedCats.push($(this).val());
        });

        $(".category_btn").click(function () {
            var arraydata = {"categories[]" : checkedCats};
            $.post('loadProducts.php', arraydata, function (data) {
                var test = $('#allProducts').html(data);
            });
        });
    };
</script>

Now if a user clicks on a product to view details and then returns to shop.php by using the back-button, he will see the main page of the shop with no dynamic loaded content. Is it possible to save the last view with marked checkboxes and loaded products? I think history.js is the solution, but I have no idea how to do this. Maybe somebody can help...

  • It's a dup of https://stackoverflow.com/questions/9046184/reload-the-site-when-reached-via-browsers-back-button go there. – Forbs Oct 20 '17 at 16:48
  • It is not. I am not adding any gets to the url so reloading the page does nothing. – Louis DeVito Oct 20 '17 at 16:55
  • When the back button is pushed, the website goes to it's original state. The URL I linked mentions how to force a reload so that you can see your items. I don't know if history.js can solve your problem, I looked at it and didn't see that – Forbs Oct 20 '17 at 17:19
  • Mhm maybe it doesn't, I'm not sure. But a reload does not check the checkboxes, so I only see the main page of the shop with no category limitations. – Louis DeVito Oct 20 '17 at 17:23
  • Oh, that's a much harder item as you would have to ajax the setttings somewhere and then the reload would pull them back. Using Back Button with AJAX settings causes lots of issues – Forbs Oct 20 '17 at 18:34
  • Do you have any ideas how to do so? Convert selected checkboxes to a get variable with javascript and add it to url and then rebuild settings with jquery from url? It would be a lot of work, because I have a lot of settings on the page (price, category, collection, color...). It would be great if there is another solution. – Louis DeVito Oct 20 '17 at 22:10

0 Answers0