1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PHP: make a selection, perform an action

Discussion in 'Web Design & Development' started by snorkytheweasel, Oct 31, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. snorkytheweasel

    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
    }
     
  2. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    just pass the file path to unlink.
     
  3. Fyzbo

    Fyzbo

    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.
     
  4. snorkytheweasel

    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?

     
  5. Fyzbo

    Fyzbo

    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.
     
  6. snorkytheweasel

    snorkytheweasel Thread Starter

    Joined:
    May 3, 2006
    Messages:
    215
    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)
     
  7. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    I've learned so much by looking at code written by other people.
     
  8. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/645797

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice