Changing the value of a variable when selecting an option

Asked

Viewed 205 times

0

In my product table, I would like to make some basic filters, such as sort by alphabetical order, price, etc...

The idea is as follows, when the user selects a option (does not necessarily need to be an option), change the value of an X variable, also changing the query in my SQL.

Example: (Only for you to understand better)

<form action="" method="POST">
    <select>
       <option value="a">Ordem A-Z</option>
       <option value="z">Ordem Z-A</option>
       <option value="preco">Ordem por preços</option>
    </select>
</form>
                 
<?php
    if (isset($_POST["a"])){
        $ordenar= "nome_produto";
    }
    if (isset($_POST["z"])){
        $ordenar= "nome_produto DESC";
    }
    if (isset($_POST["preco"])){
        $ordenar= "valor";
    }

// A ideia é mudar o valor diretamente na consulta, ao selecionar algum option...
$sql = "SELECT * FROM produtos ORDER BY $ordenar";
?>

This example, it’s incomplete, it’s just an example, but I think it gave you to understand what I want to do.

  • It is possible to do this with pure PHP?
  • How would I do something like that that works?
  • There is another way more "practical" or more certain than this?
  • It would work if I used Jquery?

I thank you for any kind of help, a great day at all.

  • I believe that the best option would be using jquery ajax, so whenever the option was changed you looked for sql in a separate php file according to the selected option, another thing I noticed is q your <select>...</select> is wrong, "name" is missing" and in your php you would have to call the $_POST according to the "name" and not according to each "value" like you did

  • I thought of this alternative, but for that I would have to put one button of type submit within the form Isn’t it? You don’t have the possibility of me doing this without pressing a button? Make the filter just by selecting the option...

  • the way I passed you wouldn’t need any button, you could use jquery’s own change() to check if <select> was changed and so do ajax

  • <select onchange="document.getElementsByTagName('form')[0].submit()">...</select>

  • This snippet will send the form when changing the option... It’s not the best way, but it should work

  • @Maybe you can give me an example in practice? I’m a little layman with js...

Show 1 more comment

2 answers

1

You can add an Event Listener or directly into the element, a call to the form Submit:

<form action="#" method="POST" name="filtro">
    <select onchange="document.getElementsByTagName('form')[0].submit()">
       <option value="a">Ordem A-Z</option>
       <option value="z">Ordem Z-A</option>
       <option value="preco">Ordem por preços</option>
    </select>
</form>

In this case, I selected by tag name, but in more realistic scenarios you should select by id or class.

Below you can run the code to see how it looked:

/**
  Simulando envio, ignore
**/

var onSubmit = function(e){
  alert('form enviada');
  return false;
 };

document.getElementsByTagName('form')[0].submit = onSubmit;
<form action="#" method="POST" name="filtro">
    <select onchange="document.getElementsByTagName('form')[0].submit()">
       <option value="a">Ordem A-Z</option>
       <option value="z">Ordem Z-A</option>
       <option value="preco">Ordem por preços</option>
    </select>
</form>

  • Thanks for the reply, that sends the form without needing a button, however, how would I get into the condition of IF? (select the option and bring a value to the variable)

  • If I can understand it, it doesn’t change anything. By submitting the form, you’re already getting the values of the $_POST. To keep the selected value, simply add the property selected in the option you want: <option value="a" <?php if(isset($_POST["a"]) echo 'selected'; ?>>Ordem A-Z</option>

  • No, pear. You need to name it pro select, and pick it up. No $_POST["a"]

  • Would look like this in html: <select name="filtro">...</select> and in php: $_POST["filtro"]

  • 1

    Thanks for the answer, I ended up doing a little different but I got based on your help! I will put my answer below, but I will consider yours. + 1

0


I was able to sort the table that way

<div class="sorting">
    <form action="" method="POST">
        <select name="filtro" onchange="this.form.submit()">
            <option value="a">Ordem A-Z</option>
            <option value="z">Ordem Z-A</option>
            <option value="preco">Ordem por preços</option>
        </select>
    </form>
</div>

<?php
$ordernar = 'ID_produtos DESC';

if (isset($_POST["filtro"])){
    $filtro = $_POST['filtro'];

    if ($filtro == "a") {
        $ordernar = 'produto';
    }
    if ($filtro == "z") {
        $ordernar = 'produto DESC';
    }
    if ($filtro == "preco") {
        $ordernar = 'valor';
    }
}

$sql = "SELECT * FROM produtos ORDER BY $ordernar";
?>

Browser other questions tagged

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