Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submit two forms with one submit button jQuery

Tags:

jquery

forms

I have tried multiple ways of doing this, by searching on stackoverflow for the answer.

I have two forms

Form 1 basic setup:

<form action="/post" class="frm-form"  method="post" name="post" 
onsubmit="return vB_Editior['text_editor'].prepare_submit(0,0)" id="quick_reply">
   <textarea></textarea>
      <input type="submit" name="post" value="Send">
</form>

Form 2 basic setup:

<form enctype="multipart/form-data" id="tagnotif" style="display:none;" 
onsubmit="return vB_Editor['text_editor'].prepare_submit(0,0)" name="post2" 
method="post" action="/privmsg">
   <input id="username" class="post" type="text" tabindex="1" size="25" 
       name="username[]"style="margin:1px 0"><br />
   <input id="acpro_inp10" class="post" type="text" 
     onkeypress="if (event.keyCode==13){return false}" title="" tabindex="2" 
    maxlength="64" size="45" value="You have been tagged" name="subject"><br />
  <textarea id="textingnotification" rows="15" cols="9"
   name="message" tabindex="3"></textarea>
   <input type="hidden" value="" name="lt">
     <input type="hidden" value="inbox" name="folder">
       <input type="hidden" value="post" name="mode">
</form>

I just need these two forms to be submitted on SEND press of first form

TRIED USING THIS CODE VARIABLES WITH THE MAIN SCRIPT: Though it won't pass both, and it won't refresh the page...

$(document).ready(function() { 
    $('#quick_reply').ajaxForm(function() { 
            alert("Thank you for your submitting form 1"); 
    });
    $('#tagnotif').ajaxForm(function() { 
            alert("Thank you for your submitting form 2"); 
    });

}); 

function mySubmitFunction() {
    $("#quick_reply").submit();
    $("#tagnotif").submit();
}
like image 695
EasyBB Avatar asked Nov 19 '12 02:11

EasyBB


2 Answers

I don't think you can do two regular post requests from a single html page, because making the post involves leaving the page and following the redirect response.

If you're submitting the forms via ajax, why not just make a single ajax button and grab the needed parameters to stick in the request? IE:

$.ajax({
  type: 'POST',
  dataType: 'JSON',
  data: {param1: $('input#param1').val(), param2: $('input#param2').val()},
  url: 'url to post to',
  success: (your success callback function goes here)
})
like image 76
Andrew Avatar answered Sep 28 '22 09:09

Andrew


Doing this first you need to remove every submit buttons from these two forms If form have submit button this won't work.

First Form

<form action="/post" id="frm-form" class="frm-form"  method="post" name="post" 
onsubmit="return vB_Editior['text_editor'].prepare_submit(0,0)" id="quick_reply">
   <textarea></textarea>
      <input id="frm-submit" type="button" name="post" value="Send">
</form>

Second Form

<form enctype="multipart/form-data" id="tagnotif" style="display:none;" 
onsubmit="return vB_Editor['text_editor'].prepare_submit(0,0)" name="post2" 
method="post" action="/privmsg">
   <input id="username" class="post" type="text" tabindex="1" size="25" 
       name="username[]"style="margin:1px 0"><br />
   <input id="acpro_inp10" class="post" type="text" 
     onkeypress="if (event.keyCode==13){return false}" title="" tabindex="2" 
    maxlength="64" size="45" value="You have been tagged" name="subject"><br />
  <textarea id="textingnotification" rows="15" cols="9"
   name="message" tabindex="3"></textarea>
   <input type="hidden" value="" name="lt">
     <input type="hidden" value="inbox" name="folder">
       <input type="hidden" value="post" name="mode">
</form>

JQuery

$("#frm-submit").change(function(){
    $("#frm-form").submit();
    $("#tagnotif").submit();
});

This will Refresh the page and submit two forms

like image 40
Thusitha Wickramasinghe Avatar answered Sep 28 '22 10:09

Thusitha Wickramasinghe