Two actions in the same form

0

Hello! I need to have two actions in the same form. I researched some solutions and did the following:

<form id="formnewsletter" name="formnewsletter" method="POST">

   <input type="text" name="nome" id="nome" />
   <input type="text" name="email" id="email" />

   <button type="button" onClick="enviaForm();">Enviar</button>

<script>
function enviaForm(){
    document.getElementById('formnewsletter').action = 'form_newsletter.php';
    document.getElementById('formnewsletter').submit();

    document.getElementById('formnewsletter').action = 'https://www.rdstation.com.br/api/1.2/conversions';
    document.getElementById('formnewsletter').submit();
}
</script>

</form>

The problem is that it only triggers the second action, if I reverse the position of the actions, then the other is not working. How could I correct this? With Ajax would it be better?

Thanks in advance!

    
asked by anonymous 22.08.2014 / 21:00

2 answers

2

I was able to send the form to two actions. If someone needs it, follow the code below:

    <script type="text/javascript">
        function enviaForm(nome,email){
            $.ajax(
                    {
                      type: "POST",
                      url: "form_newsletter.php",
                      data: "&nome="+nome+"&email="+email,
                      beforeSend: function() {
                      },
                      success: function(txt) {
                        if(txt.status=='success')
                            alert("E-mail cadastrado com sucesso!\n\nObrigado!");
                        else
                            alert("E-mail não cadastrado.");

                            EnviaForm2();
                      },
                      error: function(txt) {
                      }
                    }
                );

        }


        function EnviaForm2()
        {
            document.getElementById('formnewsletter').action = 'https://www.rdstation.com.br/api/1.2/conversions';
            document.getElementById('formnewsletter').submit();
        }
    </script>
    
01.09.2014 / 05:38
2

Your code will do the POST for only one of the two URLs, and then you will be redirected to the page that the form was sent to. See this sample .

When you use .submit() in js the browser will do the POST request and redirect to the page where the form was sent, as if the user had normally clicked on the submit form.

In your case the best thing to do is to make the requests with Ajax and then show a message to the user saying that he has been registered in both newsletters. In jQuery you can use ajaxComplete .

    
22.08.2014 / 21:34