Send more variable "serialized" form via AJAX?

Asked

Viewed 398 times

0

I want to serialize my form, and send another variable. It is possible?

Example:

var id = "1";

function register(){
  $.ajax({
    method: "post",
    url: "meu_script.php",
    data: $("#form").serialize(), //Aqui eu queria passar a variável *id*
  });
}
  • One option could be to put an input type Hidden, but I want to pass this variable via AJAX itself.

1 answer

0


You can do it like this:

var ID = 100;
function register() {
    var data = $('#form').serializeArray();
    data.push({name: "id", value: ID});
    $.ajax({
        type: 'POST',
        url: 'meu_script.php',
        data: $.param(data),
    });
}

Using the $.stop to serialize the form and add more values

  • Boaaa! I hadn’t thought of that. Valeuu!

Browser other questions tagged

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