Pass Form + Variable by AJAX

Asked

Viewed 35 times

0

People I’m trying to pass my FORM + the OFFSET variable, but I’m not getting it.. Can someone give me an example?

$("#btn-pass").click(function() {

    offset = offset + 5;
    pagina++;

    var data = $("#form-fatura").serialize();

    data.push({
        off: offset
    });

    $.ajax({
        type: 'POST',
        url: 'code-source/return/ret_info_fatura.php',
        data: data,
        async: true,
        dataType: 'json',
        beforeSend: function() {
            $("#btn-pass").prop('disabled', true);
        },
        success: function(response) {
            if (response.codigo == "1") {

                $("#btn-pass").prop('disabled', false);
                $("#btn-back").prop('disabled', false);
                $("#container-fatura").html(response.fatura_animal);
                $("#pagina").html("Página: " + pagina);
            } else {
                $("#btn-pass").prop('disabled', true);
                offset = offset - 5;
                pagina--;
                $("#btn-back").prop('disabled', false);

            }
        }
    });
});

2 answers

2

One possibility is to use the method SerializeArray

var data = $("#form-fatura").serializeArray();
data.push({name: "off", value: offset});

1


If you give a.log(data) console, you’ll see how the data is organized with a pattern like this [nameTag]=[value], if you have an input <input type="text" name="nome" value="Vinícius"> in your browser console will be printed nome=vin%C3%ADcius.

If there are 2 or more inputs, the data will be separated by the character &, that is, if there were two inputs:

<form id="formulario">
<input type="text" name="inputa">
<input type="text" name="inputb">

<input type="button" id="button" value="enviar">
</form>

The console will display inputa=valueDoInput&inputb=valueDoInputb.

In your case just add the date "&off="+offset or

data += "&off=" + offset;

Browser other questions tagged

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