How do I add? Sort = p.price & amp; order = DESC to the end of the line when the button is clicked?

  • 0
    I have a button to sort by price. I need to make it so that when I clicked on this button, it works in ascending order, and when it is repeated again, it works in descending order. I have this in my function:
    When you add? Sort = p.price & amp; order = ASC to the end of the address bar, the sort will be in ascending order. And when you add? Sort = p.price & amp; order = DESC to the end, the sorting will be in descending order. I need to make sure that when I click on the sort button, it adds? Sort = p.price & amp; order = ASC to the end of the line. And when again on this button then -? Sort = p.price & amp; order = DESC. It is necessary to implement this so that two values ​​are connected to one button, which must be added to the end of the line. Perhaps this needs to be done somehow using Javascript?
    I need to do this on my site. I found this on someone else's site - https://xi.express/catalog/smartfony/xiaomi_redmi_ ... it's there sorting by the price I want. But what kind of Javascript code is there is not clear.

    I'll be very thankful.
    JavaScript Audrey Hernandez, Apr 19, 2019

  • 3 Answers
  • 0
    Alternatively:

    const toggleOrder = () => {
    const params = new URLSearchParams(window.location.search);

    params.set('order', params.get('order') === 'DESC' ? 'ASC' : 'DESC');
    window.location.search = params.toString();
    };


    Example . Code .
    Anonymous

  • 0
    There are several ways for development.



    1 - you look at GET through php and form a backlink using the variables.

    For example

    if ($ _GET ['order'] == 'ASC') {

    $ url = 'DESC';

    }

    and you substitute the ending into the link.



    2 - you form 2 links and by clicking one you hide the other, you show it with styles (provided that your information is displayed via ajax
    Anonymous

  • 0
    Front-end solution:

    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!