1

how would I submit one form to two different location without ajax (cross-domain issue)

I was thinking of something like below. Only how would I pass params ?

   < form action="urlOne.com/something" method="post" onsubmit="postToUrl("urlTwo.com/something");">
        <input type="text" value="hello" name="hi"/>
        <input type="submit" value="submit">
    </form>

function taken from here >> JavaScript post request like a form submit

function postToUrl(url, params)

        {
            var form = $('<form>');
            form.attr('action', url);
            form.attr('method', 'POST');

            var addParam = function(paramName, paramValue){
                var input = $('<input type="hidden">');
                input.attr({ 'id':     paramName,
                             'name':   paramName,
                             'value':  paramValue });
                form.append(input);
            };

            // Params is an Array.
            if(params instanceof Array){
                for(var i=0; i<params.length; i++){
                    addParam(i, params[i]);
                }
            }

            // Params is an Associative array or Object.
            if(params instanceof Object){
                for(var key in params){
                    addParam(key, params[key]);
                }
            }

            // Submit the form, then remove it from the page
            form.appendTo(document.body);
            form.submit();
            form.remove();
        }

Thank you.

Community
  • 1
  • 1
Mega3on
  • 73
  • 1
  • 7
  • Can you do this on the server side... Id typically submit the form once to processor script which would then send the data via cURL to the two other actions. – prodigitalson Sep 25 '12 at 20:33
  • If you want to execute two posts *from the client*, you must do so explicitly. AJAX is your only option. FORM tags only define the content, and a possible target/method. – deleted_user Sep 26 '12 at 08:29

3 Answers3

4

You can only post one form at a time in a window. If you try to post two forms, one post will stop the other.

The solution would be to post the two forms to different windows. You can set the target of the first form to post to an iframe in the page or to a new window:

form.attr('target', '_blank');

This way the two posts would load in separate windows, and wouldn't stop each other.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

in case you just want to make an "invisible" post to another URL ... in this case the URL is workout.php

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
  $('form').submit(function(data) {
    $.post('workout.php', $(this).serialize());
  });
});
</script>

<form action="urlOne.com/something" method="post">
        <input type="text" value="hello" name="hi"/>
        <input type="submit" value="submit">
</form>
Reflective
  • 3,854
  • 1
  • 13
  • 25
0

You can use crossdomain ajax with jQuery if you use a proxy server-side.

or:

Use apache's mod_rewrite or mod_proxy to pass requests from your server to some other server. In your client code you just make the request as if it was actually on your server -- no browser problems with that. Apache then does its magic and makes the request to the other server for you.

Here is a link to achieve this in php: http://developer.yahoo.com/javascript/howto-proxy.html#phpproxy

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89