Caling a Function from a php generated <A HREF>

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
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Caling a Function from a php generated <A HREF>

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

You might say what you're trying to accomplish, and what you've been trying thus far...
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

here is my code

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

So my real problem is

Post 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;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need a script for delete processing, that accepts some form of data on which file(s) to delete.
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

How would I pass data from point A to point B

Post by moiseszaragoza »

How would I pass data from point A to point B
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

and my last post is pointing you in the direction to head for that.. :?
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post 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 ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you forgot a semicolon on the $from setting line. Your delete file does not call the function defined in it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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)
Post Reply