1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Opening New Window in Link, Current Window Loads new url - in PHP?

Discussion in 'PHP' started by misohoni, Nov 2, 2014.

  1. #1
    The best way I can describe it is when on retailmenot, you click a link and it opens a new window containing the information but in the previous window it loads a new url...is there a way to do it in php?

    Thanks
     
    Solved! View solution.
    misohoni, Nov 2, 2014 IP
  2. AlphaNine_Vini

    AlphaNine_Vini Active Member

    Messages:
    218
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    88
    #2
    You can set the variables within the link for example: http://example.com?varilable=2&variable=3 . So the 2 and 3 variable will be set within the php script. like $2=2 and $3=3 .When someone clich those link it will forward some information to other website. So it will redirect from your existing website to a new url will all variables set.
     
    AlphaNine_Vini, Nov 2, 2014 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    You will have to use javascripts window.location. So call a function with onclick and fist open the new window to the user and then call window.location.

    PHP will only redirect before headers are sent.
     
    Anveto, Nov 2, 2014 IP
  4. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #4
    Thanks guys, but is there a way to do it with a form? Checking the retailmenot site they use a form to submit > then opens a window > redirects a current page to the company site.
     
    misohoni, Nov 2, 2014 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #5
    Yea, you will still have to use javascript, the best way would be to open a new window and redirect onsubmit.
     
    Anveto, Nov 2, 2014 IP
    misohoni likes this.
  6. #6
    <script>
    
    function openInNewTabAndRedirect() {
        window.open("http://test.com", "test", "height=500,width=500");     // Open in a a new tab
        window.location.href = "http://website.com";                          // Redirect to this webpage
    }
    
    </script>
    
    <button onclick="javascript:openInNewTabAndRedirect();" id="action">Open a new window and redirect the current one</button>
    Code (markup):
    Here is a script that I quickly put together.
    It will first open a new window and then redirect the current window.
     
    Avener, Nov 3, 2014 IP
  7. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #7
    Thanks everyone. I set this as best answer, but have a quick question - would this work with JS turned off or on IE7 or lower?
    Would this work the same if it was in a form submit button?

     
    misohoni, Nov 3, 2014 IP
  8. Avener

    Avener Well-Known Member

    Messages:
    244
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    135
    #8
    This would not work with Javascript turned off.
    Unfortunately, there is not a way to get this done without Javascript, but it should however work with IE 7 (as long as JS is enabled) or lower as the code is very primitive.

    It would work in the same way if it was called from a form submit button.
    Simply cut the <script> to the top of the page and add
    onclick="javascript:openInNewTabAndRedirect();"
    Code (markup):
    to the submit button.


    For example:
    <input type="submit">
    Code (markup):
    Would be changed to:
    <input type="submit" onclick="javascript:openInNewTabAndRedirect();">
    Code (markup):
     
    Avener, Nov 3, 2014 IP
    misohoni likes this.
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    Personally I wouldn't do it via Javascript, but with HTML/PHP instead. A form with a _blank target should give you the new window effect. Then have PHP redirect via headers to where you want to go.
     
    ThePHPMaster, Nov 4, 2014 IP
  10. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #10
    I sort of think you're right so the submit would _blank and change current new window - but how to do in PHP?
     
    misohoni, Nov 4, 2014 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    Personally given that it's usability and accessibility crap, I wouldn't be shoving new windows down the user's throat in the first blasted place; you know, 80%+ of why the target attribute was deprecated in STRICT sixteen years ago? Using scripting to replicate an already frowned upon behavior is even worse, as it's completely missing the point.

    User wants a new window, let them choose that (middle-click, control-click) instead of removing that choice. EVERY time a website does that it just pisses me off.
     
    deathshadow, Nov 5, 2014 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    Well... in certain cases it's okay to not having to ctrl-click - for Google-search, for instance, or for pages with repeat items that you want to look at - picture galleries, stores, etc. To me, having it default open in a new tab is a good thing.
    I do however agree - normally there's no reason for forcing this on users.
     
    PoPSiCLe, Nov 5, 2014 IP
  13. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #13
    I was going to label it to notify users that it opens a new window, I'm basing it on the retailmenot model which when you click on a coupon, the current window changes to the destination and the new window opens up with a retailmenot site.
     
    misohoni, Nov 5, 2014 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #14
    I have no clue what "retailmenot" is, but what you describe is EXACTLY the type of bullshit that makes me as a user of websites automatically navigate away from site that pull such stupid inaccessible painfully unusable "gee ain't it neat" script-tard stunts -- and a hefty part of why at this point I have little more than a giant middle finger for the entire web development community as a whole. I click on a **** coupon I want to go to the **** coupon, not have some other scam artist bullshit open in a new window!

    Sorry, MAJOR pet peeve when it comes to usability. Things like what you describe are EXACTLY the type of thing that makes me whip up the knife-hand ready to deliver a pimp slap to other developers.
     
    deathshadow, Nov 5, 2014 IP
  15. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #15
    It is pretty simple, you need to somehow communicate the link between Javascript and PHP (for example, if you send PHP #1 it should redirect to google.com, if you send it #2 it redirects to yahoo.com). Redirection in PHP is not hard at all, look at this link:

    http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php
     
    ThePHPMaster, Nov 5, 2014 IP
    misohoni likes this.