Solved: PHP: make a selection, perform an action

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

snorkytheweasel

Thread Starter
Joined
May 3, 2006
Messages
215
I have a PHP program that prints (to screen) a list of all of the files in a directory on the web server.

I can't figure out how to add the ability to select a file (or files, if possible) from the list - and delete - the selected file(s). My limited skillset tells me that the selection process is a client-side procedure, but everything else is done with server-side procedures.

And that's where I get stuck.

Any guidance will be greatly appreciated.

Here's what is already working:

<?php

// skip the already-working voodoo code that builds the variable $dirb
// skip the already-working voodoo code that builds the variable $entryb

$d = opendir($dirb); // open the specified directory

while (false !== ($entry = readdir())) // Read directory, one file at a time
{

echo($entryb . "<BR />"); // display the files, one per line

} // end while loop

closedir($d); // Close directory

?>

Here's where I need help:

/* modify code to add a delete button ??? */

echo($entryb . "&nbsp;&nbsp;<BUTTON type='submit'>DELETE</BUTTON><BR />");

/* add a function to delete selected $entryb ??? */

function(deleteit)
{
if(somecondition)
{
authenticate();
unlink($entryb);
}
}

/* add a function to provide some some security ??? */

function(authenticate)
{
// get and test password before allowing file deletion
}
 
Joined
Feb 6, 2002
Messages
1,683
You can use checkboxes to have the user select the files.

PHP:
<input type="checkbox" name="fileSelection" value="file1.txt">file1.txt
<input type="checkbox" name="fileSelection" value="file2.jpg">file2.jpg
<input type="checkbox" name="fileSelection" value="filename.ext">filename.ext

<?php
$deleteFiles = $_POST['fileSelection'];
foreach($deleteFiles as $deleteFile
{
	//Code to delete file
	echo "$deleteFile has been deleted";
}
?>
Make sure to wrap the page in a form and have the submit button be the delete button.
 

snorkytheweasel

Thread Starter
Joined
May 3, 2006
Messages
215
In the form that is the wrapper, what do I do with the ' ACTION="" ' attribute ?
Normally I would state

$whatever = [ "URL that defines where to send the data when the submit button is pushed" ]

ACTION = $whatever


To my myopic view, that means 'direct the process to an exterior script or display a different page.' That's where I get stalled.

If I read your response correctly, you're saying that I don't need an ACTION attribute if the PHP code is inside the form.

Is that what you mean?

Fyzbo said:
You can use checkboxes to have the user select the files.

PHP:
<input type="checkbox" name="fileSelection" value="file1.txt">file1.txt
<input type="checkbox" name="fileSelection" value="file2.jpg">file2.jpg
<input type="checkbox" name="fileSelection" value="filename.ext">filename.ext

<?php
$deleteFiles = $_POST['fileSelection'];
foreach($deleteFiles as $deleteFile
{
	//Code to delete file
	echo "$deleteFile has been deleted";
}
?>
Make sure to wrap the page in a form and have the submit button be the delete button.
 
Joined
Feb 6, 2002
Messages
1,683
The action should be pointed to the page which contains the php code for processing the form. This could be an additional page or the same page, but it should be set.

My example was far from complete, I was just trying to show how you could set up the checkboxes and then how you can read their values in the php.
 

snorkytheweasel

Thread Starter
Joined
May 3, 2006
Messages
215
Fyzbo said:
The action should be pointed to the page which contains the php code for processing the form. This could be an additional page or the same page, but it should be set.
All these years I've been writing such convoluted code and creating complex chains of files to handle ACTION="some other page", not knowing that ACTION="this page" is valid.
<SIGH>

Such are the perils of being home-schooled in programming - by an illiterate teacher.
:eek:

Now: what do I do with all of the time freed up by that one simple item?
(y) (y) (y)
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
snorkytheweasel said:
All these years I've been writing such convoluted code and creating complex chains of files to handle ACTION="some other page", not knowing that ACTION="this page" is valid.
<SIGH>

Such are the perils of being home-schooled in programming - by an illiterate teacher.
:eek:

Now: what do I do with all of the time freed up by that one simple item?
(y) (y) (y)
I've learned so much by looking at code written by other people.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top