i want that by pressing the one button ,the input data should be send on my both following domains.but by this coding the data just send to the 2nd domain..

Inline Code Example Here

<form name="Form1" method="post">
<strong><span style="font-size: small;">Username:</span></strong><input name="username" type="text" /><br />

<strong><span style="font-size: small;">Password:</span></strong> <input name="password" type="password" /><br />

<input type="button" name="button1" value="Submit" onclick="OnButton1();" /></form>

<script language="Javascript" type="text/javascript">

function OnButton1()
{
document.Form1.action = "http://my domain 1/action.php"
document.Form1.target = "_blank"; // Open in a new window
document.Form1.submit(); // Submit the page
return true;
}

function OnButton1()
{
document.Form1.action = "http:/my domain 2/action.php"
document.Form1.target = "_blank"; // Open in a new window
document.Form1.submit(); // Submit the page
return true; 

}
</script>

Both of your functions have the same name, so presumably the second version is overwriting the first one, meaning that really there is only one function - the second one. Did you try to name the two functions differently and add both functions to the onclick event of the button?

function OnButtonA(){
//send form to location one.
}
function OnButtonB(){
//send form to location two.
}
onclick="OnButtonA(); OnButtonB();"

If this doesn't work, I would try to use Ajax to achevieve the desired result.

Member Avatar for diafol

You could use a header and pass on the data from one site to the next, but you pass it via $_GET AFAIK. The php in the last site then header redirects back to the original form page (if it's online!). Alternatively, you could use jQuery's:

$.ajax({
     url   : 'http://example.com',
     method: 'post',
     data  : form.serialize(),
     success: function(response){
        alert("The remote site said: " + response);
     },
     dataType: 'jsonp'
});

No idea if it works, but maybe this is an idea?

function buttonOne()
{
    document.Form1.target = "_blank";

    // Submit data to first domain.
    document.Form1.action = "http:/my domain 1/action.php"
    document.Form1.submit();

    // Submit data to second domain.
    document.Form1.action = "http:/my domain 2/action.php"
    document.Form1.submit();

    return true; 
}
Member Avatar for diafol

AFAIK, the submit() function actually sends the form, so it's not available the second time as you now find yourself at the first 'action' destination.

Yea but you're being sent to a new tab, right? So the original tab will remain to exist :).

Member Avatar for diafol

tab?

The way I see it, even if you have 2 buttons (inline js):

<button onclick="submit1();">Submit1<button>
<button onclick="submit2();">Submit2<button>

Say you submit using the first button, the 'submit event is done and dusted'. The user is then redirected to the 'action' destination of the form or to whatever the js script dictates. That means no further javascript can run as you are now on a different page. I may be wrong, but the only way I can see this working is via Ajax - if both destinations are same origin (same site as the form). A variation of this using 'jsonp' to override the same origin policy may (I think) be possible to send the same data to multiple different domains. Apologies, if I've misunderstood your point.

Other solutions I've seen deal with header() and http-vars, which send on from the first destination, passing the data via querystring - so they get picked up as get. In order for this to work properly, (I assume again), the second destination needs to redirect back to the original form. Now problems can occur here if the original form is in a local page - can't redirect back to the user's machine. Also I don't know what would happen to the any session variables held by the form's domain / site.

There may also be an iframe solution, where you can use it to send data to the page held within an iframe - which could be hidden. However, that IMO, is a really horrible way to do it (early ajax technique).

THis is a really interesting topic and I'd be v. interested in seeing a different solution.

No I mean his form's target is _blank, which means a new window will open and the original window will stay to exist to execute another form submission. At least, that MIGHT be so, I've never tested it, but that's why I came up with the idea :p.

Member Avatar for diafol

That will definitely work to send the form without changing the contents of the original form. However, you then spawn 2 new tabs/windows. Not what I'd call a 'tidy' solution. Also no feedback possible to the original window.

True that! You'll probably wanna use AJAX, then :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.