Mega3on
Mega3on

Reputation: 65

submiting one form to two different URLs without ajax

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.

Upvotes: 1

Views: 3117

Answers (3)

Reflective
Reflective

Reputation: 3917

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>

Upvotes: 1

Armel Larcier
Armel Larcier

Reputation: 16037

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

Upvotes: 0

Guffa
Guffa

Reputation: 700592

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.

Upvotes: 4

Related Questions