How to send two actions in a single form?

Asked

Viewed 2,315 times

2

Hello I have a pagseguro button on my site, but when I click on it I want it to take the form data and send it to another page (to then put it in the database). So when you click on the pagseguro button it will send two action. (one for pagseguro and one for my bd.

Payment form.

<!-- INICIO FORMULARIO BOTAO PAGSEGURO -->
<form action="https://pagseguro.uol.com.br/checkout/v2/cart.html?action=add" method="post" onsubmit="PagSeguroLightbox(this); return false;">
<!-- NÃO EDITE OS COMANDOS DAS LINHAS ABAIXO -->
<input type="hidden" name="itemCode" value="DF45A4323B3BA53224E48FBA638D48CE" />
<input type="image" src="https://p.simg.uol.com.br/out/pagseguro/i/botoes/pagamentos/209x48-pagar-assina.gif" name="submit" alt="Pague com PagSeguro - é rápido, grátis e seguro!" /> 
</form>
<script type="text/javascript" src="https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.lightbox.js"></script>

Website form.

<form method="post" action="">
            <table class="tb">
                <tr><td>Nome: </td><td><input type="text" name="nome"></td></tr>
                <tr><td>E-mail: </td><td><input type="text" name="email"></td></tr>
                <tr><td>Telefone: </td><td><input type="text" name="telefone"></td></tr>
            </table>
 </form>

I’m thinking of putting both in the same form, but the question continues. How to send to two different pages via action?

2 answers

2

By the standard of W3C, since Voce submits a form, Oce has already navigated and can no longer submit exactly the same form, why Voce defines only one target in the action. A workaround for your case may be:

  1. First send the data of your form via Ajax to record in your database
  2. Then you can submit the form normally to the pagseguro, as Voce is doing now
  • I thought the same thing, maybe it’s the best way to do it. Fill out the form and then go to the pagseguro.

  • It is certainly better even, send ajax and then in Success send to pagseguro

2


Hello,

You can only submit the form once to the given url. To resolve your problem you can submit these requests via ajax.

https://stackoverflow.com/questions/19800343/posting-mvc-razor-form-from-ajax

Example :

<form>
Primeiro nome: 
<input type="text" name="firstname">
<br>
Último nome: 
<input type="text" name="lastname">
<input type="button" value="Save" id="saveName">
</form>

Script Js :

$('#saveName').click(function (event) {
    event.preventDefault();
    $.ajax({
        contentType: 'application/json'
        data: $("form").serialize(),
        type: 'POST',
        url: '<SUA URL>'
    }).done(function (data) {
       //Tratar algum retorno da sua aplicação
    });

    $.ajax({
        contentType: 'application/json'
        data: $("form").serialize(),
        type: 'POST',
        url: '<URL PAGSEGURO>'
    });
});

Browser other questions tagged

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