0

I am sending two forms at the same time but there is an error in my script. I've researched the error - Uncaught TypeError: Property 'submit' of object # is not a function. They told me to add a submit button but there is a submit button already here is my form

<form name="form1" id="form1" method="post" action="page1.php">
    <input type="hidden" name="pt" id="pt" value="<?=$b64string?>" style="width:800px; padding: 10px;">
    <input type="submit" value="submit" name="submit"/>
</form>
<form name="form2" id="form2" method="post" action="page2.php">
    <input type="hidden" name="signature" value="<?=$_sign?>"/>

    <input type="submit" value="submit" name="submit">
</form>
<span onclick="submitBoth();" style="cursor:pointer;"><img src="image1.png"></span>

Here is my jquery:

jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){
    if(data.trim()=='valid'){
        document.form1.submit();
    }
});
Kiel
  • 483
  • 3
  • 5
  • 18
  • form elements need to be inside the form to be considered part of it. I've never tried this but try putting a hidden submit inside each form. – Rick Calder Mar 14 '13 at 09:36
  • 3
    Use `$("#form1").trigger('submit');` instead of `document.form1.submit()`; – N.B. Mar 14 '13 at 09:38
  • hi @RickCalder it didn't work i tried adding `` form2 submits but form didn't i didn't get any error after. – Kiel Mar 14 '13 at 09:42
  • hi @N.B. tried using yours to form1 did not submit. Also i did not get any errors. – Kiel Mar 14 '13 at 09:45
  • i edited my title its supposed to be send it to two pages. – Kiel Mar 14 '13 at 09:48
  • i got this working using the idea of @N.B. but i changed it by triggering the submit button `jQuery('#submit1').trigger('click');` thanks! – Kiel Mar 14 '13 at 09:55

4 Answers4

0

you use 'form2' as submission URL, is that correct?

jQuery.post("form2", jQuery("#form2").serialize() ... etc

first param of jqery.post needs to be an url.

  • sorry it was a typo i edited it before placing it here. Should be form2.php – Kiel Mar 14 '13 at 09:50
  • so why dont just use smth like this? `jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){ // you sure, that response will be a string, right? if(data.trim()=='valid'){ jQuery.post("form1.php", jQuery("#form1").serialize(), function(data){ // todo: postprocess form1.php responce }); } });` – Alexander Tretyak Mar 14 '13 at 09:54
  • i've tried it awhile ago but the other server has restrictions and i think they don't accept ajax posts from other sites. i'm passing the forms to different pages and different hosts. – Kiel Mar 14 '13 at 09:57
  • but thanks for the help also! – Kiel Mar 14 '13 at 09:58
  • In case of cross-domain requests, the post below could be useful: You should follow a different pattern. Your local JS will do an ajax post to a local URL which will accept the POST method with your json data. At this point your server code will do an HTTP POST with proper data to the remote server, get the response, and send it back to the calling js. see http://stackoverflow.com/questions/6761982/jquery-cross-domain-post-shenanigans – Alexander Tretyak Mar 14 '13 at 10:01
0

so why dont just use smth like this?

jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){
   // you sure, that response will be a string, right?
    if(data.trim()=='valid'){
        jQuery.post("form1.php", jQuery("#form1").serialize(), function(data){
     // todo: postprocess form1.php responce
    });
    }
});

and sorry for previous comment markup

0

i got this working using the idea of N.B but i changed it by triggering the submit button jQuery('#submit1').trigger('click'); thanks!

Kiel
  • 483
  • 3
  • 5
  • 18
0

You can use this

function submitBoth(){
    $.post('form1.php',$('form[name=form1]').serializeArray(),function(){
          $('form[name=form2]').submit();
    });
}
Rohit Subedi
  • 560
  • 3
  • 13