Page 1 of 1

Caling a Function from a php generated <A HREF>

Posted: Fri Feb 18, 2005 10:14 am
by moiseszaragoza
Calling a Function from a php generated <A HREF>

I have been trying to attach a function to a php generated <a href> but I cant figured it out.

Posted: Fri Feb 18, 2005 10:15 am
by Maugrim_The_Reaper
You might say what you're trying to accomplish, and what you've been trying thus far...

here is my code

Posted: Fri Feb 18, 2005 10:16 am
by moiseszaragoza

Code: Select all

<?php
					$dir = "../../../myDirectory/";

				// Open a known directory, and proceed to read its contents
					if (is_dir($dir)) &#123;
				   if ($dh = opendir($dir)) &#123;
				       while (($file = readdir($dh)) !== false) &#123;  				   	      
				           echo "<a href='#'>Delete File </a> &nbsp;&nbsp;&nbsp;&nbsp;<a href='$dir$file' target='_self'>filename: $file : </a>" . "<br>";
				       &#125;
			       closedir($dh);
				   &#125;
				&#125;

feyd | :roll:

Posted: Fri Feb 18, 2005 10:22 am
by feyd
you cannot directly attach any php functions to a html link, they are on seperate sides of requests: php is server-side (at this time), and html is client-side.

The code you posted isn't trying to attach a function or any php code to a link. If anything, it'd just attempt to give a link to a file/directory in $dir.

So my real problem is

Posted: Fri Feb 18, 2005 10:22 am
by moiseszaragoza

Code: Select all

<?php
					$dir = "../../../myDir/";

				// Open a known directory, and proceed to read its contents
					if (is_dir($dir)) &#123;
				   if ($dh = opendir($dir)) &#123;
				       while (($file = readdir($dh)) !== false) &#123;  				   	      
				           echo "<a href='WHAT GOES HERE TO CALL MY FUNCTION'>Delete File </a> &nbsp;&nbsp;&nbsp;&nbsp;<a href='$dir$file' target='_self'>filename: $file : </a>" . "<br>";
				       &#125;
			       closedir($dh);
				   &#125;
				&#125;

Code: Select all

<?php
	function DeletemyFile($dir,$file)
&#123;
	unlink($dir$file)
   echo "the file : $file has been deleted from: $dir  ";
&#125;
?>

Posted: Fri Feb 18, 2005 10:25 am
by feyd
you need a script for delete processing, that accepts some form of data on which file(s) to delete.

How would I pass data from point A to point B

Posted: Fri Feb 18, 2005 10:27 am
by moiseszaragoza
How would I pass data from point A to point B

Posted: Fri Feb 18, 2005 10:32 am
by feyd
pass some form of identification via the url:

http://www.yoursite.com/deletescript.php?file=foo.gif

$_GET['file'] = 'foo.gif';


this is basic scripting 101 stuff..

Posted: Fri Feb 18, 2005 10:33 am
by moiseszaragoza
Let me try to explain what I want to do.

I have a php script displays the files I have on a directory to the browser. I want to be able to choose witch file I want to delete.

But I need to be able to choose witch file I want to delete

Posted: Fri Feb 18, 2005 10:36 am
by feyd
and my last post is pointing you in the direction to head for that.. :?

Posted: Fri Feb 18, 2005 10:48 am
by moiseszaragoza
My Script on de Show Files

Code: Select all

<?php
					$dir = "../../../mydir/";

				// Open a known directory, and proceed to read its contents
					if (is_dir($dir)) &#123;
				   if ($dh = opendir($dir)) &#123;
				       while (($file = readdir($dh)) !== false) &#123;  				   	      
				           echo "<a href='Deletefile.php?file=$file'>Delete File </a> &nbsp;&nbsp;&nbsp;&nbsp;<a href='$dir$file' target='_self'>filename: $file : </a>" . "<br>";
				       &#125;
			       closedir($dh);
				   &#125;
				&#125;
				
				
				?>



So my script on the delete page should look like :

Code: Select all

<?php 
$file = $_GET&#1111;'file'] 

	function DeletemyFile($file)
&#123;
	//unlink($dir$file)
   echo "the file : $file has been delete ";
&#125;
 ?>
But wen I run it I get an error
Parse error: parse error, unexpected T_FUNCTION in Directory/Deletefile.php on line 4

and line 4 has

1 <?php
2 $file = $_GET['file']
3
4 function DeletemyFile($file)
5 {
6 //unlink($dir$file)
7 echo "the file : $file has been delete ";
8 }
9 ?>

Posted: Fri Feb 18, 2005 3:46 pm
by feyd
you forgot a semicolon on the $from setting line. Your delete file does not call the function defined in it.

Posted: Fri Feb 18, 2005 5:48 pm
by John Cartwright
Also $dir will not be read within the function. You must pass the variable through the function call like you did with $file

ie. DeletemyFile($file,$dir)