简体   繁体   中英

Is it possible how to two forms submit by one button and then two forms perform two different actions?

I have to tried JavaScript.But it performs one form action only.Please explain how to an one button using two forms in same page.

I have to use the below code:

 function my() { document.schedule.action = "firstaction"; document.schedule.target = "_blank"; document.schedule.submit(); return true; } function my1() { document.consultant.action = "secondaction"; document.consultant.target = "_blank"; document.consultant.submit(); return true; } 

Parallax form submissions are not possible

Because form submission means loading new page, so browse close all exiting connections like your previous form submission.

solution

Use ajax

function my()
{
   $.ajax({
    url:document.schedule.action,
    type:'post',
    data:$(document.schedule).serialize()
  });  
 return true;
}

submit() would submit the form the normal way, thus reloading the page before the other/second form could be submitted. To do what you want use Ajax A jQuery Ajax example:

$("#form1").submit(function(e){
    e.preventDefault();
    $.post("functions.php", {key1:"value1", key2:"value2"}, function(data){
        //submit the second form here using the same $.post method
    });
});

Ajax is the solution.

You can do something like this:

<form class="myform">
  <!-- form 1 fields -->
</form>

<form class="myform">
  <!-- form 2 fields -->
</form>

And using JQuery

$('.myform').sumit(function(e)) {
    e.preventDefault();

    // FORM VALIDATION

    // Form 1 function
    $.ajax({
        url: "action1",
    }).success(function() {
        // DO SOMETHING
    });

    // Form 2 function
    $.ajax({
        url: "action2",
    }).success(function() {
        // DO SOMETHING
    });
}

Btw in my opinion you don't need anymore two forms but just one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM