javascript - How to post form to two different pages with a single button click? -


i have form single button below:

<form name="sampleform" id="sampleform" method="post" action="" enctype="multipart/form-data"> <input type="text" id="biosample" name="biosample" class="sample"/> <input type="text" id="library" name="library" class="sample"/> <input type="submit" name="btnadd" id="btnadd" class="buttonsub" value="next>>"> </form> 

ajax code is:

<script> $(document).ready(function(){     var encoded_project_id = $('#encoded_project_id').val();     $('#sampleform').on('submit', function(){     var target = 'windowformtarget';     window.open('', target, 'width=500,height=300');     this.setattribute('target', target);     $.post('postdata.php', $(this).serialize(), function(){       window.location.href = 'phases.php?edit='+encoded_project_id;     }).fail(function(){     window.location.href = 'sample.php?edit='+encoded_project_id;     });   }); }); </script> 

now when button clicked, want post data above form in 2 pages - handler.php , postdata.php

handler.php should open in new javascript window , postdata.php should open in same tab , same window.

how can achieved?

edit: seem using jquery, change this:

$(document).ready(function () {     document.getelementbyid('sampleform').onsubmit = function (e) {         var req = new xmlhttprequest();         req.open('post', 'test.php', true);         req.send();     } }); 

to this:

$(document).ready(function(){   $('#sampleform').on('submit', function(){     $.post('postdata.php', $(this).serialize(), function(){       console.log('success');     }).fail(function(){       console.log('error');     });   }); }); 

you should 2 things. first add

<form target="_blank" action="handler.php"></form> 

this ensure when submit button clicked form open new window.

then need intercept submit so:

document.getelementbyid('sampleform').onsubmit = function(e){   //xmlhttprequest function   //this send form postdata.php } 

the code above called first , can send form asynchronous xmlhttprequest object postdata.php . after function ends, default behavior of form start , handler.php receive form.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -