Send post with multiple values through JQuery

0

I have this JQuery code to send a Post.

var formTest = document.createElement('form');
formTest.setAttribute("method", "post");
formTest.setAttribute("action", "");
var post = document.createElement("input");
post.setAttribute("type", "hidden");
post.setAttribute("name", "idsTransf");
post.setAttribute("value", "25");
formTest.appendChild(post); 
document.body.appendChild(formTest);
formTest.submit();

In this case, I send a post with the value of "idsTransf". How do I send the post with multiple values through this code?

    
asked by anonymous 18.04.2017 / 02:02

1 answer

1

From the data in form

post.id = "submit";

First you will need to get the data of this form, right after doing the append in the document

var _url = "caminho/para/o/arquivo.php"
var _data = new FormData($("#submit")[0]);

So you can use ajax function from form

$.ajax({
        url: _url,
        type: 'POST',
        data: _data,
        timeout: 20000,
        processData: false, 
        contentType: false,
        success: function (data) {
            //Código para resposta da requisição
        }, error: function(data){
            //Código para falha na requisição
        });

processData: false and contentType: false - > does not transform date into Json format for FormData, since it is already a FormData

You can also pass an array but you should use the following code

$.ajax({
        url: _url,
        type: 'POST',
        data: {name1:array(valor1,valor2),name2:array(valor3,valor4)},
        timeout: 20000,
        success: function (data) {
            //Código para resposta da requisição
        }, error: function(data){
            //Código para falha na requisição
        });

processData and contentType - > in default setting transforms date into Json format for FormData

In case you do not need to create form

    
18.04.2017 / 02:40