简体   繁体   中英

How to use jQuery within PHP

I'm using a php tree browser that I got from http://www.lateralcode.com/directory-trees-with-php-and-jquery/

<?php
 $path = ".";

 function createDir($path = '/')
 { 
  if ($handle = opendir($path)) 
  {
   echo "<ul>";

   while (false !== ($file = readdir($handle))) 
   {
    if (is_dir($path.$file) && $file != '.' && $file !='..')
     printSubDir($file, $path, $queue);
    else if ($file != '.' && $file !='..')
     $queue[] = $file;
   }

   printQueue($queue, $path);
   echo "</ul>";
  }
 }

 function printQueue($queue, $path)
 {
  foreach ($queue as $file) 
  {
   printFile($file, $path);
  } 
 }

 function printFile($file, $path)
 { 
  echo "<li><a style=\"cursor:pointer\" ".
       "onClick=\"parent.right.document.getElementById('file_path').value='".
       "$file_path'\">$file_path</a></li>";
 }

 function printSubDir($dir, $path)
 {
  echo "<li><span class=\"toggle\">$dir</span>";
  createDir($path.$dir."/");

 ?>

 **<script language="javascript">
   $(document).click(function(e) { 
   if (e.button == 0) {
    // was the left button
    alert('clicked'); 
   }
  });
  </script>**

 <?php 
  echo "</li>";

However, I want to detect a right-click that is pressed inbetween, so that I can trigger an event. However, I can't seem to get the jquery code to work within my PHP. The reason being, that I don't get an alert popup when I execute the code ... Ideally, I want to have this included within my php code, since I need to use my php variable $file_path

PHP can only process code and send output to the browser. jQuery runs strictly in the browser and will either be executed when the DOM loads it (ie, when PHP sends it and the browser receives/processes it) or when the document has finished loading (usually preferable). To do the latter use this:

$(document).ready(function() {
   $(document).click(function(e) { 
   if (e.button == 0) {
    // was the left button
    alert('clicked'); 
   }
  });
});

I'm making the assumption your code is correct because it isn't extremely clear what you're trying to do. But that might shore up some of your problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM