Filter results without refreshing the page

I’m trying to create a ‘filter by’ for a site I’m working but am not sure how to do that. I’ve already built the ‘filter by’ list and each one is able to get the results of that particular query but need to know is how to get it to change the results every time an option is click (or even removed if the button unchecked).

I’m guessing I need to use AJAX to change the results without refreshing the page but haven’t really used it before so am not sure where start

This site is the sort of thing I’m after

Yes, AJaX would be my way of doing it. I don’t have time to type out an example, for you. However, there should be plenty of documentation found on Google. Basically, use AJaX (either build your own XMLHttpRequest, or use jQuery or MooTools for AJaX) to get the results based upon the form data (checkboxes and whatnot) present, and then use the DOM to change the innerHTML (JS) or .html() (jQuery) of the results element.

HTH,

:slight_smile:

Thank you I’ll have a look for that on Google

I’m really sorry to be a pain but I wondered if you could help me with this please. I’ve searched google but haven’t been able to get this sorted yet. All of the examples I’ve come across are for forms but I have a list of links instead. I’m more than happy to pay for help with this but I’m really desperate to get it sorted now.

Thank you

If you’re using links or forms, you want to use preventDefault() so that the page won’t refresh. I’ll try to provide some pseudo-code. This hasn’t been tested.

Let’s say you have two links - Sort By Name (ASC) and Sort By Name (DESC) - and an area where the results appear. If you can get your sql recordset into JSON format or XML format, $.get() can work.

<a href="javascript:void(0);" class="sorter asc">Name Ascending</a>
<a href="javascript:void(0);" class="sorter desc">Name Descending</a>
<div id="dsplyNames">
  
</div>

Create one function that can handle both ascending and descending. Place the following within your jQuery document ready function.

$('.sorter').click(function(event){
  event.preventDefault();
  var dir = ($(this).hasClass('asc')) ? 'asc' : 'desc' ; // ternary if statement
$.get('sqlPage.php',{dir:dir}).done(function(data){ // assuming JSON, display it - you can figure out the rest
     $('#dsplyNames').html(data);
     });
  }  
);

HTH,

:slight_smile:

Each link is a different flavour so do I just change the class to add the flavour to it? Like this:

<div class="box_nav_cont">
  <div class="box_nav">&nbsp;</div>
<div class="box_nav_title"><a href="#" class="nav_title" style="cursor: default;">Flavour</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Banana" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Banana</a></div>
            <div class="box_nav"></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Butter Pecan Toffee" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Butter Pecan Toffee</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Cappuccino" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Cappuccino</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Chocolate" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Chocolate</a></div>
            <div class="box_nav"></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Cookies &amp; Cream" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Cookies &amp; Cream</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Irish Cream" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Irish Cream</a></div>
            <div class="box_nav"></div>
<div class="box_nav"><a href="javascript:void(0);" class="nav_link Mocha" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Mocha</a></div>
            <div class="box_nav"></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Peanut Butter" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Peanut Butter</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Pina Colada" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Pina Colada</a></div>
            <div class="box_nav"></div>
<div class="box_nav"><a href="javascript:void(0);" class="nav_link Tropical" class="nav_link"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Tropical</a></div>
            <div class="box_nav"></div>
                        <br>
              <div class="clr"></div></div>

Then how do I get the flavour into the JQuery? I tried this but it didn’t work

<script type="text/javascript">
$('.nav_link').click(function(event){
  event.preventDefault();
var flavour = $(this);
$.get('/products/filter/?search=' + $(flavour).val(),{dir:dir}).done(function(data){
     $('#container').html(data);
     });
  }  
);</script>

I just get an error saying:
TypeError: Can only call HTMLCollection.item on instances of HTMLCollection

Use ALT or TITLE if you want to pass a flavour on to the query. (Just my two cents.)

:slight_smile:

PS… I just noticed that many of your anchor tags have two CLASS attributes. That’s a no-no. :smile:

How would I use those in the JQuery? Sorry I don’t know much about it myself.

Oops, my mistake that was me auto replacing text - I should have checked it before posting :flushed:

I’m interested in why you think that using multiple class attributes is a no-no?

Oops - in looking over the code I see that there are literally two class attributes there, and not multiple class selectors within the one class that I thought was being mentioned.

1 Like

My take is that the problem isn’t multiple class values, but multiple class attributes eg.
...class="val1 val2 val3"
is OK
... class="val1" class="val2" class="val3"
is not

1 Like

Yes, the following must be condensed:

<a ... class="nav_link Tropical" class="nav_link" ...>

So that it has just one class attribute:

<a ... class="nav_link Tropical" ...>
1 Like

That was my mistake, I’m used find and replace to change that. The code itself isn’t like that. This is the code I’ve got but it’s still not working:

<div class="box_nav_cont">
  <div class="box_nav">&nbsp;</div>
<div class="box_nav_title"><a href="#" class="nav_title" style="cursor: default;">Flavour</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Banana"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Banana</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Butter Pecan Toffee"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Butter Pecan Toffee</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Cappuccino"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Cappuccino</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Chocolate"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Chocolate</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Cookies &amp; Cream"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Cookies &amp; Cream</a></div>
          <div class="box_nav"><a href="javascript:void(0);" class="nav_link Irish Cream"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Irish Cream</a></div>
          <div class="box_nav"><a href="javascript:void(0);" class="nav_link Mocha"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Mocha</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Peanut Butter"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Peanut Butter</a></div>
            <div class="box_nav"><a href="javascript:void(0);" class="nav_link Pina Colada"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Pina Colada</a></div>
          <div class="box_nav"><a href="javascript:void(0);" class="nav_link Tropical"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Tropical</a></div>              <div class="clr"></div></div>

WolfShade - I don’t understand what you mean about passing the flavour in the TITLE or ALT, how would I get that into the JQuery?

You can add a data-flavor attribute to the anchor tag.

<a href="javascript:void(0);" data-flavor="Tropical" class="nav_link Tropical"><img src="/images/site/filter_off.jpg" height="8" width="8">&nbsp;&nbsp;Tropical</a>

Then fetch it on click event of respective anchors -

$('.nav_link').on('click', function(){
    var flavor = $(this).data('flavor');
    .....
});

Thank you that’s working great although each time I click a flavour it changes the results, is it possible to add results instead? So if I click strawberry and then tropical it’ll show everything with both flavours?

yes that pretty much possible. Replace .html() API with .append() API of jQuery and this will append the ajax response to the container.

$('#container').append(data);

Thanks that almost works but is it possible to get it to remove what’s there first time then append everytime after?

Yup, we can define a status class on container div like “is-loaded” on success of ajax call. Then place a check if this class exists on not to differentiate between first call and subsequent calls.

Try something like this (havn’t tested this code snippet but you will get some idea out of it)

if(!$('#container').hasClass('is-loaded')) {
    $('#container').addClass('is-loaded');
    $('#container').html().append(data);
} else {
    $('#container').append(data);
}

I changed it slightly to this:

    if(!$('#container').hasClass('is-loaded')) {
    $('#container').addClass('is-loaded');
    $('#container').html(data);
} else {
    $('#container').append(data);

and it worked, but I’ve got another question. When I go to the search file (/products/filter/?search=Chocolate for example) directly it loads the correct results, but if I call it like this I don’t get any results. I’ve checked and both of them use GET

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.