Skip to content Skip to sidebar Skip to footer

How To Post Form To Two Different Pages With A Single Button Click?

I have a form with a single button like below:
).onsubmit = function (e) { var req = newXMLHttpRequest(); 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 do two things. First add

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

This will ensure when the submit button is clicked that the form will open a new window.

Then you need to intercept the submit like so:

document.getElementById('sampleForm').onsubmit = function(e){
  //xmlHTTPRequest function//This is where you send your form to postdata.php
}

The code above will be called first and you can send your form with the asynchronous XMLHTTPRequest object to postdata.php . After that function ends, the default behavior of the form will start and your handler.php will receive the form.

Solution 2:

you just to need two ajax call . Do something like this

$(document).ready(function(){
    // if want to stop default submission
    $("#sampleForm").submit(function(e){
    e.preventDefault();
    });

    $(document).on('click','#btnAdd',function(){
        send('page1.php',{'data1':'value'});
        send('page2.php',{'data1':'value'});
    });


});


functionsend(url,data)
{
    $.ajax({
        url: url,
        type: 'POST',
        datatype: 'json',
        data: data,
        success: function(data) {
            // success
        },

        error: function(data) {
            alert("There may an error on uploading. Try again later");
        },

    });


}

Post a Comment for "How To Post Form To Two Different Pages With A Single Button Click?"