Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
    
So... it's going to load redirect.php and then POST to itself? That all depends on what it is going to be POSTing, some more details about what you're trying to achieve might help. –  Mark Henderson Jan 20 '11 at 2:11
    
I want it to open redirect.php in a new window and then POST it data... –  matt74tm Jan 20 '11 at 3:48
    
What do you mean by "POST it data"? What data are you POSTing? –  Lèse majesté Jan 20 '11 at 3:54
1  
Do you just want a link that opens a POST-requested page in a new window (one request)? Or do you really want the page to open in a new window, and then a POST request to load in the same window (two requests)? –  Bavi_H Jan 20 '11 at 4:09
1  
It looks like one site can open another site in a new window, but it can't manipulate the other site once the window is open (same-origin policy). Is there some varying hidden form value on the page that you need to re-send in the POST request? If not, then perhaps the ideas in my answer below might work for you. –  Bavi_H Jan 21 '11 at 3:34
show 3 more comments

1 Answer

up vote 3 down vote accepted

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|improve this answer
    
Hi - just checking, does your second code snippet open example.com/example.php in a new window and POST name1 and name2 to it? –  matt74tm Jan 28 '11 at 1:00
    
@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.) –  Bavi_H Jan 30 '11 at 4:36
    
Thanks Bavi_H - I think that works perfectly for me –  matt74tm Feb 13 '11 at 4:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.