Open a url in a new page automatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mungo
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2003 4:25 am
Location: Stavanger, Norway
Contact:

Open a url in a new page automatically

Post by mungo »

I have a url stored in $link . How can I make the page open that url in a new window automatically on refresh (refresh is in <META http-equiv="refresh" content="330">?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can't do it using PHP you would have to use JavaScript but you can pass the PHP variable to the JavaScript code, see:
viewtopic.php?t=1030

Mac
mungo
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2003 4:25 am
Location: Stavanger, Norway
Contact:

Post by mungo »

[Edit]I don't get any parse error now, but instead it prints the link instead of open the link in a new window. It doesn't execute the javascript at all. Here's the output:

Code: Select all

Connected successfullyhttp://www.xxxx/page.php?x=226705Thank you for adding 1 follower to '.$nick.'.
[/edit]
I am getting an parse error in the line with window.open.
Here is some of the code:

Code: Select all

echo '<script language="Javascript" type="text/javascript"> 
         
        if ( 0 ) 
        &#123; 
            window.open('$row&#1111;"link"]', "outwarlink", "WIDTH=800, HEIGHT=600 ,
            resizable=yes, status=no");;
        &#125; 
         
        </script>';


        /* Printing results in HTML */
        echo 'Thank you for adding 1 follower to <B>'$nick'</B>.'; 
and so on....
The error message i receive is:

Code: Select all

Parse error: parse error, expecting `','' or `';'' in /mnt/home3/s/st/stfjord/public_html/crewbuilder.php on line 50
what am i doing wrong? $link is containing a string like this: http://some_domain.com/some_page.php?x=456586
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Code: Select all

<html>
<title>New Window Onload</title>
<head>
<script language="JavaScript">
<!-- 
function open_link(url,name)
{ 

new_window = window.open('<?=$link?>','<?=$linktitle?>', ' menubar,resizable,dependent,status,width=300,height=200,left=10,top=10')
}
// -->
</script>
</head>

<?
if ($link){
echo "<body onload="open_link()">";
} else {
echo "<body>";
}
?>
Your new window will open when this document loads 
</body>
</html>
try this. I cutomized it from this url for reference.
mungo
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2003 4:25 am
Location: Stavanger, Norway
Contact:

Post by mungo »

still not working. See for your self: http://home.no.net/stfjord/crewbuilder.php

here's the code:

Code: Select all

<META http-equiv="refresh" content="330">
<HTML>
<TITLE>::Pheonix Army's CrewBuilder::</TITLE>
  <HEAD>

<script language="JavaScript"> 

<!--
function open_link(url,name) 
&#123; 

new_window = window.open('<? $link ?>','<? $nick ?>')
&#125; 
// --> 
</script> 

    
  </HEAD>
  
    <? 
	//11 entries in database so far. Will have to make it find number of
        //entries automatically later when it grows big.


        $firstEntry=1;
        $lastEntry=11;
        $randNr = rand (1, 11);

        /* Connecting, selecting database */
        $db = mysql_connect("some_server", "user", "passw")
        or die("Could not connect");
        print "Connected successfully";
        mysql_select_db("outwar") or die("Could not select database");
        /* Performing SQL query */
        $query = "SELECT nick, rank, link FROM members WHERE nr='$randNr'";
        $result = mysql_query($query) or die("\nQuery failed");
        // While a row of data exists, put that row in $row as an associative array
        // Note: If you're expecting just one row, no need to use a loop
        // Note: If you put extract($row); inside the following loop, you'll
        //       then create $userid, $fullname, and $userstatus
        $row = mysql_fetch_assoc($result);
        extract($row);
	
/*	//Proving that nick, link and rank is extracted from DB:
        echo $nick;
        echo $link;
        echo $rank;
*/
	$linktitle=$nick;

	if ($link)&#123; 
	echo "<body onload="open_link()">"; 
	&#125; else &#123; 
	echo "<body>"; 
	&#125; 

       
        /* Printing results in HTML */
//        echo 'Thank you for adding 1 follower to <B>'; echo $nick;echo'</B>.'; 
    /* Free resultset */
    mysql_free_result($result);
//    /* Closing connection */
//    mysql_close($link);
        /* Disconnecting from database*/
        $connStatus = (mysql_close($db)) or die("Terminating database connection failed");
?>
      </BODY>
</HTML>
Are you sure I shouldn't have any arguments in the call to open_link in <body onload=.....>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Very odd indeed.

I did a few tests and i came to the conclusion that meta refreshing does not work if the variable that the meta tag is going to forward to is defined within the page. Like so:

Code: Select all

<?php
$self = $PHP_SELF;
<meta http-equiv="refresh" content="5; url=<?=$self?>?link=http://forums.devnetwork.net"> 
?>
It only worked for me when i used it with the GET method (put it in the url).

Here is the working source:

http://oromian.port5.com/test-javapopup ... ource=true

Here is the working site:
http://oromian.port5.com/test-javapopup ... apopup.php

and here it is with a link loaded

http://oromian.port5.com/test-javapopup ... p://smx.ca

Enjoy.
Post Reply