2 Replies - 1139 Views - Last Post: 06 July 2011 - 07:41 PM Rate Topic: -----

#1 Astrumas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 05-July 11

Deleting Multiple Images, One Form

Posted 06 July 2011 - 01:49 PM

I am creating a simple PHP script that displays all the images contained within a folder and adds the ability to delete them by clicking the button.

I have figured out how to do this, but I believe I have made it more complicated than it needs to be. Here is a snippet of my code containing the image/button generating code:

if (is_dir($dir))
    {
        if ($dh = opendir($dir))
        {
            while (($file = readdir($dh)) !== false)
            {
                if ($file != "." && $file != "..")
                {
                    echo "<form name=mgph method=post action=upload_file.php>";
                    echo "<input type=hidden name=dir value=$dir>";
                    echo "<img src=$dir$file />";
                    echo "<input type=hidden name=del value=$dir$file>";
                    echo "<input type=submit value=Delete>";
                    echo "</form>";
                }
            }
            closedir($dh);
        }
    }


It works fine I am just curious to know if it is bad practice to have every image have its own form, and if so, what would be better?

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting Multiple Images, One Form

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Deleting Multiple Images, One Form

Posted 06 July 2011 - 04:54 PM

Yes it's bad practice and the right way to do it would be to use one form with a list of images, each having its own checkbox. Checkbox tutorial
Was This Post Helpful? 1
  • +
  • -

#3 Astrumas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 05-July 11

Re: Deleting Multiple Images, One Form

Posted 06 July 2011 - 07:41 PM

View PostJackOfAllTrades, on 06 July 2011 - 04:54 PM, said:

Yes it's bad practice and the right way to do it would be to use one form with a list of images, each having its own checkbox. Checkbox tutorial


Awesome thank you, whenever I learn how to code I always worry about having bad code practices, thanks for the advice!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1