0

I have an array of texts which I have kept in an array. The array is linked to a button and when that button is pressed, I'd like to open all the links in different tabs.

e.g

if(isset($_POST["open links"]))
{

foreach($array as $item)
{
   <a href="$item" target="_blank" ></a>
}
}

The links are saved on a text file from a previous form and each item in the array is just the URL. How would I go about doing this?

  • PHP is server side and cannot control the browser. Javascript has some control over the browser, but not the way you want http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript. – vascowhite Jan 10 '15 at 10:59
  • In your title you're asking to turn an array of text into links then, in the question, you want to actually open a tab for each link. What do you want exactly? are the links already displayed? Take a PHP tour first in any case, if you want to actually **open** some tabs PHP is not the way to go, but you rather want to use javascript. If you need to display the links, however, this PHP code is NOT correct and should not even compile, because it has many syntax errors. – briosheje Jan 10 '15 at 11:00

4 Answers4

0

How about trying to echo them

foreach($array as $item)
{
   echo('<a href="' . $item . '" target="_blank" ></a>');
}

It prints the link tags with values of $item as target.

Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
0

How about this

<?php foreach($array as $item)
      { 
?>
        <script>
              window.onload = function(){
                  window.open("<?=$item?>", "_blank"); // will open new tab on window.onload
              }
       </script>

<?php } ?>
Naved Munshi
  • 487
  • 1
  • 5
  • 17
0

To open multiple links at the same time you will need some (basic) javascript.

Try something like this:

<?php

$array = array( 'http://www.stackoverflow.com', 'http://www.google.com');

?>

<button id="my-button">Click me</button>

<script type="text/javascript">

    var links = [
        <?php
            foreach($array as $i => $link)
                echo '"' . $link  . '"' . ($i < (sizeof($array) -1)? ',' : '');
            ?>
        ];
    document.getElementById("my-button").onclick = function(){
        links.forEach(function(link) {
             window.open(link, '_blank');
        });
    }

 </script>

Note that Chrome popup blocker doesn't let you programmatically open multiple new tabs at once, though. (Window.open isn't working for multiple links in Google Chrome)

Community
  • 1
  • 1
Fromage
  • 33
  • 4
  • Thank you, I'm new to coding and haven't learned javascript yet. I'm about to try this. If Chrome won't let you open multiple tabs at once, is there a way around this? – Smanga Simelane Jan 10 '15 at 11:25
  • As far as I know there is no way around this. You can disable the popup blocker for your page though. – Fromage Jan 10 '15 at 11:34
0

consider your link file :-

links.txt

> http://google.com http://stackoverflow.com http://facebook.com
  • All links with sperated by space

then php code :-

<button id="my-button">Click me</button>

<script type="text/javascript">

   document.getElementById("my-button").onclick = function(){
       <?php  foreach($links as $link) {    echo"window.open(" . $link . ", '_blank');"; } ?>
   }

</script>

Some code stolen from other answers :p , but that's a good practice ! Thanks :)

Tiger
  • 404
  • 1
  • 4
  • 13