3 forms in a single button

Asked

Viewed 775 times

1

Good afternoon, I have a code on html and js where I need to make a Submit in 3 forms using just one button, that’s because they’re on different cardboards. I thought I’d do it this way in html:

<form id="form1" method="POST"></form>
<form id="form2" method="POST"></form>
<form id="form3" method="POST"></form>

<button id="submit-forms" type="button" class="btn btn-success>

And in the JS:

$("#submit-forms").on("click" function(){
    $("#form1").submit();
    $("#form2").submit();
    $("#form3").submit();
});

But it didn’t work out. Does anyone have any idea of a solution?

  • 1

    Where does java enter? There is nothing java in this code.

  • When a form is sent the page is "reloaded" to the action, then I only see two ways, put it all together in one <form> only and handles it on the server, or send it by javascript with AJAX.

  • In fact, it makes sense yes, because my Divs, created for both page layout and organization, did not allow to be made only a form @Sam

  • has this little Gambi here. Just change the page in the dynamic form action.

2 answers

-1

You could send it via Ajax with Jquery and pick up the data via GET (if it doesn’t interfere with your application’s security):

var dados = '?'+$('#form1').serialize()+'&'+$('#form2').serialize()+'&'+$('#form3').serialize();

$.ajax({
    url: pagina+dados,
    success: function(response){
    },
    error: function(response){
    }
});

A little more about serialize: https://api.jquery.com/serialize/

-2

In the JS file try to do the following:

$("#form1").submit(function(event) {
    action(event);
});

$("#form2").submit(function(event) {
    action(event);
});

$("#form3").submit(function(event) {
    action(event);
});

function action(event)
{
    //ação requerida
}

The Event will be triggered after clicking the Submit button. You do not need to put the "Submit" event capture methods inside a "click".

Browser other questions tagged

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