Bring values from a select with AJAX automatically

Asked

Viewed 34 times

1

I started using AJAX for a while and I have a question. I own a <select> and I am trying to bring the value of it on the same page (without giving refresh), however, it is only displayed the value when I click on <select> again, to select another option. How would you show as soon as I selected?

Suppose I select the <select> with value="4", only "4" is shown when I click on <select> again...

index php.:

<select class="common_selector" id="ordenar_tabela">
     <option value="1">Lançamentos</option>
     <option value="2">Menor valor</option>
     <option value="3">Maior valor</option>
     <option value="4">A - Z</option>
     <option value="5">Z - A</option>
</select>

<script>
$(document).ready(function(){

    filter_data();

    function filter_data()
    {
        var action = 'fetch_data'; 
        var ordenacao = $('#ordenar_tabela').val();

        $.ajax({
            url:"verificar.php",
            method:"POST",
            data:{action:action, ordenacao:ordenacao},
            success:function(data){
                $('.filter_data').html(data);
            }
        });
    }

    $('.common_selector').click(function(){
        filter_data();
    });

});
</script>

check.php:

<?php
          $query = "SELECT * FROM produtos_estoque";
            
          // Se o usuário quiser ordenar a tabela...
          if(isset($_POST["ordenacao"])){
        
          $ordenacao = $_POST["ordenacao"];
          echo $ordenacao; //Estou exibindo essa variavel na página, para ver se vai funcionar os demais códigos abaixo...
        
          // $query .= ($ordenacao == 1) ? " ORDER BY ID_produtos DESC" : "";
          // $query .= ($ordenacao == 2) ? " ORDER BY ID_produtos DESC" : "";
          // $query .= ($ordenacao == 3) ? " ORDER BY ID_produtos DESC" : "";
          // $query .= ($ordenacao == 4) ? ' ORDER BY produto' : "";
          // $query .= ($ordenacao == 5) ? ' ORDER BY produto DESC' : "";
        }
?>
  • I’ll edit it by showing the $query, $('.filter_data') was not included in the question.

1 answer

-1


Your problem is happening because you call the function filter_data() when the <select> is clicked.

See how to call the function when selecting a new option using the attribute onchange:

<select class="common_selector" id="ordenar_tabela" onchange="filter_data()">
    <option value="1">Lançamentos</option>
    <option value="2">Menor valor</option>
    <option value="3">Maior valor</option>
    <option value="4">A - Z</option>
    <option value="5">Z - A</option>
</select>
function filter_data()
{
    var action = 'fetch_data'; 
    var ordenacao = $('#ordenar_tabela').val();

    $.ajax({
        url:"verificar.php",
        method:"POST",
        data:{action:action, ordenacao:ordenacao},
        success:function(data){
            $('.filter_data').html(data);
        }
    });
}

Another option, keeping HTML intact and using jQuery:

<select class="common_selector" id="ordenar_tabela">
    <option value="1">Lançamentos</option>
    <option value="2">Menor valor</option>
    <option value="3">Maior valor</option>
    <option value="4">A - Z</option>
    <option value="5">Z - A</option>
</select>
function filter_data()
{
    var action = 'fetch_data'; 
    var ordenacao = $('#ordenar_tabela').val();

    $.ajax({
        url:"verificar.php",
        method:"POST",
        data:{action:action, ordenacao:ordenacao},
        success:function(data){
            $('.filter_data').html(data);
        }
    });
}

$(".common_selector").change(function() {
  filter_data();
});
  • I edited the question, I will use the value that <select> in a PHP condition, thus changing my page query.

  • In the verificar.php you are returning the value of the POST, ie the value sent. The code remains redundant. If you want to return the query values, it is a question about SQL, not AJAX. Please rephrase your question to clarify your goal.

  • Theoretically it’s working, I’m getting the values and it’s being displayed correctly. The problem is, it’s just displaying, when I click on <select> again, I think this is how I’m getting the value with AJAX...

  • Right. I’ll rephrase the answer.

  • Unfortunately, it didn’t work, I didn’t see any change...

  • Using the attribute onchange, when you select an option on <select> it will call the function. Your code is using the onchange? Confirming, your backend is returning the information correctly, right?

Show 2 more comments

Browser other questions tagged

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