Send two variables with AJAX to update query

1

I am generating a table by divs, some filters with some selects. At the moment I can decide how many records I want to show, but I have to send more variables, for example the select that I ordered by price. I show the AJAX that I have, if someone knows how to send more than one variable. Thanks

    $(function(){
    $("#registros, #ordenar, #tipo").on("change", function(e){
      var registros ="";
      var ordenar ="";
      var tipo ="";

        if ($("#registros").val() !=null){
          registros = $("#registros").val();
        }
        if ($("#ordenar").val() !=null){
          ordenar = $("#ordenar").val();
        }
        if ($("#tipo").val() !=null){
          tipo = $("#tipo").val();
        }

        $.ajax({
            url: "filtros.php",
            type: "post",
            dataType: "html",
            data: {
          'registros': registros,
          'ordenar': ordenar,
          'tipo': tipo
       },
            beforeSend: function(){
              $("#res").html("Procesando, espere por favor...")
            },
            success: function (resultado){
              $("#res").html(resultado);
            }
    });
});

    });

Then in the other file that generates the query is like this.

$results = $mysqli->query("SELECT productos.id, productos.producto, 
productos.imagen, productos.alias, posibles.idproducto, posibles.tipo, 
posibles.entrada, posibles.mensualidad, posibles.final, posibles.total 
FROM productos, posibles WHERE productos.id = posibles.idproducto AND 
posibles.tipo =" . $_POST["tipo"]. " ORDER BY posibles.final $orden 
$limit");
    
asked by Miguel 03.08.2018 в 09:07
source

2 answers

0

If I'm not mistaken, what you're looking for is sending more than one variable in the AJAX, for that you only have to add these variables to the JSON you send, to data

data: {
    "registros": $("#registros").val(),
    "Dato": "Dato",
    "Otro_Dato": "otro_dato"
}

It would be as you have the records but with the new data to be sent and adding the price id so that the function will also run when its select change, for example with the price your code would look something like this:

$(function(){
    $("#registros, #precio").on("change", function(e){
       $.ajax({
           url: "filtros.php",
           type: "post",
           dataType: "html",
           data: {
              'registros': $("#registros").val(),
              'precio': $("#precio").val()
           },
           beforeSend: function(){
             $("#res").html("Procesando, espere por favor...")
           },
           success: function (resultado){
             $("#res").html(resultado);
           }
       });
   });
});

Make sure they are always separated by commas minus the last entry you enter.

You should keep in mind the default value of the selector. If $("#precio").val() of first is null could give you problems. Make sure to add a value to the initial option so that it does not happen.

Another way to avoid this problem would be by passing variables initialized in the data instead of the pure value. That is:

$("#registros, #precio").on("change", function(e){
    var registros = "";
    var precio = "";

    if ($("#registros").val() != null) {
        registros = $("#registros").val();
    } 

    if ($("#precio").val() != null) {
        precio = $("#precio").val();
    }

    /* AJAX */
}

And in the data, pass these variables instead of the data as such:

data: {
    "registros": registros,
    "precio": precio
}

In this way the variables will never be null and the code will read it well.

As extra data: By having the Ajax in a onchange you do not need to put it in another function. Obviously I do not know if there is more code or some reason for it but, just as the code is now, the first and last line would not be necessary, the onchange is already creating a function in itself.

    
answered by 03.08.2018 / 09:16
source
0

Dento of the data tag separated by commas

data: {'registros': $("#registros").val() , 'valor2': valor2}
    
answered by 03.08.2018 в 09:13