manianis manianis - 1 month ago 7
HTML Question

send the form values to multiple pages

I am working on a web application where I need to send the value from one form to more than one page.

My form is simple :

<form method="post">
<input type="text" name="name" value="">
...
</form>


I want the values from the form to be sent to :


  • 'page1.php' which will replace the actual page containing the form displaying the message : data saved

  • 'page2.php' which will open when I click the submit button to render the data as a PDF file



The problem is that I can only specify only one target using the action attribute of the form.

Anyone can helps ? Thanks in advance.

Could I use JavaScript or jQuery or other ?

Answer

You can do it using JavaScript and jQuery for example. The easiest way is send two ajax requests:

$('form').submit(function() {
    $.ajax({
        async: false,
        method: 'POST',
        url: 'page2.php',
        data: $( this ).serialize(),
        success: function() {
             window.open("http://www.stackoverflow.com"); //this page will be open in new tab
        }
    });

    $.ajax({
        async: false,
        method: 'POST',
        url: 'page1.php',
        data: $( this ).serialize(),
        success: function() {
            window.location.href = "http://stackoverflow.com"; //redirect to this page
        }
    });
});