Sorting ASC and DESC without losing variable value

4

I am trying to perform a sort order of records on a page by firing a List / Menu, but when I fire it, I am losing variable values from a first selection.

When I enter the products page, I already place a $ dep variable and show the result. When trying to make the filter by ASC or DESC the page is reloaded, thus losing the previous search and does not perform the ordering.

I made a few attempts, saved the variables in sessions, and tested 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'm triggering the filter's value for the products.php page and it looks like this:

And my List / Menu looks like this:

<select name="filtro" id="filtro" onchange="valueselect(this.value);">
    
    function valueselect(filtro)
    {
          window.location.href = "produtos.php?filtro="+filtro;
    }
      
                    
asked by anonymous 28.01.2015 / 14:46

1 answer

4

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

$filtro = $_GET['filtro'];

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

$filtro = $_REQUEST['filtro'];
    
28.01.2015 / 14:51