Can I POST to a new window that I want to open up?

45,231

If you just want a link that opens a POST-requested page in a new window here are some ideas. However, be aware a POST request can't be bookmarked.

  • You can make a button that opens a POST-requested page in a new window.

    <form method="post" action="http://example.com/example.php"
      target="_blank">
    <input type="hidden" name="name1" value="value1">
    <input type="hidden" name="name2" value="value2">
    <input type="submit" value="Open results in a new window"> 
    </form>
    
  • If you want it to look like a link, you can make a link with an onClick attribute that submits the form.

    <form name="myform" method="post" action="http://example.com/example.php"
      target="_blank">
    <input type="hidden" name="name1" value="value1">
    <input type="hidden" name="name2" value="value2">
    </form>
    
    <a href="http://example.com/example.php"
      onClick="document.forms['myform'].submit(); return false;">Open results
      in a new window</a>
    

    The onClick part submits the form, which opens the POST-requested page in a new window. The return false prevents the browser from also going to the href address in the current window at the same time. If Javascript is disabled or the link is bookmarked, the href address is used as a fallback, but the resulting page won't receive any POST values. This might be confusing or unfriendly for your users if they bookmark the link.

If you want the link to be bookmarkable, investigate if your page can accept GET parameters. If so, then you can make a bookmarkable link.

 <a href="http://example.com/example.php?name1=value1&name2=value2"
   target="_blank">Open results in a new window</a>
Share:
45,231

Related videos on Youtube

siliconpi
Author by

siliconpi

Updated on September 17, 2022

Comments

  • siliconpi
    siliconpi over 1 year

    Is this possible?

    1. A third party site is running my Drupal module
    2. An end user clicks on a link which will:
    3. Open up a new window www.mysite.com/redirect.php
    4. and POST certain data to this page www.mysite.com/redirect.php

    I've seen the user's browser being redirected, but am not clear on how to do the above.

  • siliconpi
    siliconpi about 13 years
    Hi - just checking, does your second code snippet open example.com/example.php in a new window and POST name1 and name2 to it?
  • Bavi_H
    Bavi_H about 13 years
    @matt_tm I've been confused by your wording "open in a new window and POST to it". The first example above sends one POST request to the example.com server, the response of which the web browser displays in a new window. If Javascript is enabled, the second example does exactly the same thing as the first example, but looks like a link instead of a button. (When Javascript is enabled, the link's href is ignored, only the onClick is used. The return false tells the browser to ignore the href.)
  • Danielle
    Danielle over 2 years
    What worked for me was placing the target="_blank" under the <form> element... instead of placing on the <a>....