tags:

views:

2279

answers:

5
<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" >
</form>
<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow.com/" >
</form>
<a onclick="test(event, '1'); " href="#" >Click Here</a>
<script>
    function test(event, id){
        document.getElementById("frm_"+id).submit;
        document.getElementById("tgt_"+id).submit;
    }
</script>

Is it possible to open a new tab/window and change the current page ?

A: 

As far as I know, it's not possible to submit two forms at once. Since you're using PHP however, why not take a look at the cURL library? It lets you send POST and GET requests and parse the results in PHP.

To answer the question in the title, if you simply want to open two pages with one click, you could do it like this (excuse the inline javascript):

<a
    href="http://www.google.com"
    target="_blank"
    onclick="document.location.href='http://www.yahoo.com'"
>Click here to open Google in a new window and yahoo in this window</a>
nickf
A: 

You should use the window.open to open the new page and then submit the tabchange e.g.

<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" >
</form>
<a onclick="test(event, '1'); " href="#" >Click Here</a>
<script>
    function test(event, id){
        window.open("http://stackoverflow.com/", "_blank");
        document.getElementById("tgt_"+id).submit();
    }
</script>
kouPhax
A: 

Use the ‘target’ attribute on the form elements to make them submit to eg. a new window or an iframe, without reloading the current page (breaking any other form submissions).

Your a.onclick should also return false to stop the href link being followed. Really this should be a button not a link.

Avoid this sort of stuff unless you've got a specific good reason. Depending on what you're actually trying to do there is usually a better way.

bobince
A: 

Still trying this end.

Usage of <a> was to have the browser status bar show the href url.

My original code works when the href param is href="local_page.php" and only document.getElementById("tgt_"+id).submit; exists.

BUT this means that I am unable to POST to the local page any info about the chosen option.

In this case using GET and appending parameters is not an option.

John Griffiths
+1  A: 
<form id="frm_1" name="frm_1" target="_self" method="POST" action="local_page.php" >
<input type="hidden" name="vital_param" value="<?= $something ?>">
</form>

<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow.com/" >
</form>

<button type="submit" onclick="test(event, '1'); " >Click Here</button>

<script>
    function test(event, id){
    window.open( document.getElementById("tgt_"+id).action, "_blank");
    setTimeout('document.getElementById("frm_'+id+'").submit();', 1000);

    return true;
    }
</script>

tgt kept as source of target url, could be array or attribute. Without setyTimeout() browser stays on current page (FF/IE).

John Griffiths