ASC and DESC sorting without losing variable value

Asked

Viewed 407 times

4

I am trying to perform a sort of records on a page by firing a List/Menu, but by firing the same I am losing values of variables from a first selection.

When entering the products page I already position a $Dep variable and show the result. When trying to make the filter by ASC or DESC the page is reloaded, thus losing the previously done search and does not perform the ordering.

I made some attempts, saved the variables in sessions and test the value of the filter variable:

    $dep = $_REQUEST['dep'];
    $subdep = $_REQUEST['sub'];
    
    $_SESSION['dep'] = $dep;
    $_SESSION['sub'] = $subdep;
    
    $filtro = $_POST['filtro'];

    switch ($filtro) {
        case 1:
            $ORDER = " ORDER BY produtos.descricao ASC";
            break;
        case 2:
            $ORDER = " ORDER BY produtos.descricao DESC";
            break;
        default:
            $order = " ORDER BY produtos.descricao ASC";
    }

When selecting a filter option I am triggering the value of the same for the page products.php and the same is so:

And my List/Menu is like this:

<select name="filtro" id="filtro" onchange="valueselect(this.value);">
    
    function valueselect(filtro)
    {
          window.location.href = "produtos.php?filtro="+filtro;
    }
      

The site for display is this: Website in Development

  • If there are several combos ex: parents, city, state it is better to use ajax to avoid refreshing the screen. If only one you do one on <select> compare the database items with the request value in this case add selected="selected" at the time of writing the <option>

  • Hello @rray, I’m not using ajax and I believe this is the problem, but I can’t find an example of list/menu with this feature, thanks for the help.

1 answer

4


When you load the page by changing the window.location.href, you make a request of the type GET, and not POST. But in PHP you are looking for the value in $_POST. If you always use GET (that is, if the information never arrives via posting a form), use $_GET in PHP:

$filtro = $_GET['filtro'];

If the request type varies, you can use $_REQUEST, that combines the information of POST, GET and Cookies (but be careful if there is a second key 'filtro' somewhere else):

$filtro = $_REQUEST['filtro'];
  • Hello @bfavaretto, thank you for the tip, I have remade so much this code that went unnoticed, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.