Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combining php and javascript code

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
US
I am using a php script to list the websites [folders] in a directory and links to them. I am trying to figure out a way to make another window open when the link to a given folder is clicked with javascript.

I have tried placing the javascript code in multiple places with no luck.

Any help given wouldbe greatly appreciated.

Thanks

Code:
<?php

$dir = '/var/[URL unfurl="true"]www/html/userz/';[/URL]
// show directory content
function showDir($dir, $i, $maxDepth){
   $i++;
   if($checkDir = opendir($dir)){
       $cDir = 0;
       $cFile = 0;
       // check all files in $dir, add to array listDir or listFile
       while($file = readdir($checkDir)){
           if($file != "." && $file != ".."){
               if(is_dir($dir . "/" . $file)){
                   $listDir[$cDir] = $file;
                   $cDir++;
               }
               else{
                   $listFile[$cFile] = $file;
                   $cFile++;
               }
           }
       }
       
       // show directories
       if(count($listDir) > 0){
           sort($listDir);
           for($j = 0; $j < count($listDir); $j++){
               echo "
               <tr>";
                   $spacer = "";
                   for($l = 0; $l < $i; $l++) $spacer .= "&emsp;";
                   // create link
                     $link = "<a href=\"" . "[URL unfurl="true"]http://www.memblog.com/userz"[/URL] . "/" . $listDir[$j] . "\">$listDir[$j]</a>"; 
 echo "<td>" . $spacer . $link . "</td>
               </tr>";
               // list all subdirectories up to maxDepth
               if($i < $maxDepth) showDir($dir . "/" . $listDir[$j], $i, $maxDepth);
           }
       }
       
       // show files [changed the'>' to '<' in the line below to not show the files
       if(count($listFile) < 0){
           sort($listFile);
           for($k = 0; $k < count($listFile); $k++){
               $spacer = "";
               for($l = 0; $l < $i; $l++) $spacer .= "&emsp;";
               echo "
               <tr>
                   <td>" . $spacer . $listFile[$k] . "</td>
               </tr>";    
           }
       }        
       closedir($checkDir);
   }
}

if($_GET["dir"] == "" || !is_dir($_GET["dir"])) $dir = getcwd();
else $dir = $_GET["dir"];
// replace backslashes, not necessary, but better to look at
$dir = str_replace("\\", "/", $dir);

// show parent path
$pDir = pathinfo($dir);
$parentDir = $pDir["dirname"];

// echo "<a href=\"" . $_SERVER["PHP_SELF"] . "\"><h3>Home</h3></a>";
// echo "Current directory: " . $dir;
// echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=$parentDir\"><h4>Parent directory: $parentDir</h4></a>";

// Display directory content
echo"<center><table border=1 cellspacing=0 cellpadding=2>
<tr><th align=left>Username / Blog</th>";

// specifies the maxDepth of included subdirectories
// set maxDepth to 0 if u want to display the current directory
// $maxDepth = 0;
showDir($dir, -1, $maxDepth);  
?>

javascript
Code:
onclick="window.open(this.href, '_blank'); return false;"
it works with other links:
Code:
<a href="[URL unfurl="true"]http://mysite.com/tour/screenshot1.jpg"onclick="window.open(this.href,[/URL] '_blank'); return false;">
 
what do you mean with no luck ?? you get an error message or it's simply not working ??

in php code you must always add an \ before the " sign for instance if the htmlcode is in a print statement

and I can't connect you're problem with all the code provided above

and why use javascript for this and not just the _new option ?
<a href=blabla.html target="_blank">

 
i inserted the javascript
Code:
onclick="window.open(this.href, '_blank'); return false;">
behind this line

Code:
$link = "<a href=\"" . "[URL unfurl="true"]http://www.memblog.com/userz"[/URL] . "/" . $listDir[$j] . "\">$listDir[$j]</a>";

like so
Code:
$link = "<a href=\"" . "[URL unfurl="true"]http://www.memblog.com/userz"[/URL] . "/" . $listDir[$j] . "\" onclick="window.open(this.href, '_blank'); return false;"> $listDir[$j]</a>";

and got the following error: Parse error: parse error, unexpected T_STRING in /var/ on line 33
 
you forgot the slashes for "

$link = "<a href=\"" . " . "/" . $listDir[$j] . "\" onclick=\"window.open(this.href, '_blank'); return false;\"> $listDir[$j]</a>";
 
thanks a million!!!!!


greatly appreciated hos2, greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top