RA
7 0
Asked
Updated
Viewed
51.4k times

Basically what I want to do is this:

<form method="POST" action="https://www.google.com/asdf.cgi" action="http://www.asdf.com/blah.pl">

Obviously, you cannot have multiple action attributes in an HTML form tag, but it's the idea that I am after. I have had trouble finding a proper method to do this so that it actually submits to both actions on submit.

add a comment
1

3 Answers

  • Votes
  • Oldest
  • Latest
Answered
Updated

Honestly, I would probably write a script that you POST to, and then behind the scenes, your script would handle any dirty work of submitting the values/content to multiple providers. That would solve multiple problems and simplify your code so that you don't even need to use Javascript. However, I am not going to go into the details of how to write that script.

Instead to solve the actual question here you cannot achieve this via HTML alone (as previously mentioned), however, you can achieve this using Javascript. The main issue is that when you submit on HTML the page takes you to a new location and we cannot do that otherwise we are only going to be submitting to one of the destinations. So there are two ways we can do this:

  1. Use Javascript to post Forms on two iframes
  2. Use AJAX which allows you to POST without changing your current page

Javascript POSTing using Iframes

You could do this via having two iframes on the page where you have JavaScript target a form submit in each iframe:

let myForm = document.getElementById('subscribe');

myForm.action = 'https://www.google.com/asdf.cgi';
myForm.target = 'iframe_1';
myForm.submit();

myForm.action = 'http://www.asdf.com/blah.pl';
myForm.target = 'iframe_2';
myForm.submit();

AJAX POSTing

An easier way may simply be to use AJAX to POST multiple times to your destination. Here is a quick and dirty example that could probably be cleaned up so you re-use code with functions:

// Any data you want sent with the POST request
let data = {
    field1: 1,
    field2: 'a string',
}

let postData = JSON.stringify(data);
let action = 'https://www.google.com/asdf.cgi';
let action2 = 'http://www.asdf.com/blah.pl';

let xhr = new XMLHttpRequest();
xhr.open('POST', action, true)
xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8')
xhr.send(postData);

xhr = new XMLHttpRequest();
xhr.open('POST', action2, true)
xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8')
xhr.send(postData);
add a comment
0
Answered
Updated

I didn't have time to test this extensively, but this is what I came up with off the top of my head, and it worked when I tried to submit it to two different formmail scripts:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function doubleSubmit(f) 
{
    // submit to action in form
    f.submit();

    // set second action and submit
    f.target="_blank";
    f.action="formmail2.php";
    f.submit();
    return false;
}
//-->
</script>
</head>
<body>
<form method="post" action="formmail.php" onsubmit="doubleSubmit(this)">
<input type="text" name="myTextBox"><br>
<textarea name="myTextArea"></textarea>
<input type="submit" value="Double Submit">
</body>
</html>
add a comment
0
Answered
Updated

I don't have time to write out the code, but the way I would probably handle this is by, instead of using a form submit button, I would use a regular button with an onClick that populates some global JS variables with your form data, then opens two small popups.

Those popups would contain a form with only hidden fields that get populated by an onLoad function from the opener variables and then submits with the last action being a window.close();.

When the person clicks submit, these two windows popup and then close (best to put window.blur() on both, or window.focus() on the form window). Then, use a while loop (careful to avoid infinite loops...) to watch those two windows, and once both are closed, location.href to a thank you page.

Unfortunately, as you know, there is no way to do what you are talking about in pure HTML.

The other way is to chain your Perl scripts, so that, at the end of doing whatever the first one does, it forms an HTTP request and sends it to the second one. I've had a bit of success with doing this, though I was sending XML streams via HTTP from a Perl script to an ASP script (heh, don't ask, I one found myself writing a Perl script, to create ASP code that printed out JavaScript code that manipulated HTML. Makes you're head hurt just to think about it, and escaping quote marks was a bloody nightmare!)

add a comment
0