Redirect page with post using ajax

1

I know the question is very strange, but it is a need that we have, I need to move to another page of some variables that I have redeemed, but it is not just passing but redirecting the page as well. I can not use a window.location, it needs to be per post.

What I have at the moment, the return is showing me the variables that I need to pass, but as I said I need to redirect.

function preparaPagina() {

    $('button.inline-button').click(function() {

        var self = $(this);
        var IdCota = self.siblings('.product-info').data('idcota');
        var IdProduto = self.siblings('.product-info').data('produto');
        var IdUsuario = self.siblings('.product-info').data('usuario');
        var NomeUser = self.siblings('.product-info').data('nome');

        dados = {"IdCota": IdCota, "IdProduto": IdProduto, "IdUsuario": IdUsuario, "NomeUser": NomeUser  };

    $.ajax({
        type: 'POST',
        url: 'iCadAutorizacaoTrans.php',
        data : dados,
        success:function(data){
            console.log(data);
        }
    });
    })
}

preparaPagina();
    
asked by anonymous 12.07.2018 / 23:19

1 answer

1

In the success function of Ajax, you can enter a form on the page by placing the values in each input with its name and submitting it via method="post" , redirecting the page to the declared destination page in the variable url_ (see explanatory comments in the code):

function preparaPagina(){

   $('button.inline-button').click(function(){

      var self = $(this);
      var IdCota = self.siblings('.product-info').data('idcota');
      var IdProduto = self.siblings('.product-info').data('produto');
      var IdUsuario = self.siblings('.product-info').data('usuario');
      var NomeUser = self.siblings('.product-info').data('nome');

      var dados = {"IdCota": IdCota, "IdProduto": IdProduto, "IdUsuario": IdUsuario, "NomeUser": NomeUser  };

      // url do post
      var destino = 'iCadAutorizacaoTrans.php';

      $.ajax({
         type: 'POST',
         url: destino,
         data : dados,
         success:function(data){
            console.log(data);

            // monta o formulário
            // note que o atributo "hidden" faz com que ele fique oculto
            var formulario = '<form id="form" hidden method="post" action="'+destino+'">'
            +'<input name="IdCota" value="'+IdCota+'">'
            +'<input name="IdProduto" value="'+IdProduto+'">'
            +'<input name="IdUsuario" value="'+IdUsuario+'">'
            +'<input name="NomeUser" value="'+NomeUser+'">'
            +'</form>';

            // insere o formulário no body
            $("body").append(formulario);
            // faz o submit e remove o formulário
            $("#form").submit().remove();

         }
      });
   })
}

preparaPagina();
    
13.07.2018 / 02:00