Performing different actions on dynamically generated buttons. PHP
<p>I am working on a PHP Gallery application, and need some help here. Actually I have a page where images from a specific directory are displayed directly. With each one of the images displayed there is a dynamically generated submit button that will be used to delete respective images separately. </p>

<p>Every image has its own submit button, that will be used to delete that image. Being new to php I need some method that can be called to delete only that image from the actual or physical directory.</p>

<p>There is a similarity between image and button that I have coded it such that every image and its respective button has names such as "img_1" and its button is "del_1".</p>

<pre><code>&lt;form id="albumGallery" name="albumGallery" method="POST"&gt;
&lt;?php

   $dir =  htmlspecialchars($_GET["p"]) . "/";
    $imgs = array();

    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
        if (!is_dir($file) &amp;&amp; preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
            array_push($imgs, $file);
        }
    }

    closedir($dh);
} else {
    die('cannot open ' . $dir);
}
$i=0;
echo "&lt;div id='images'&gt;";
foreach ($imgs as $idx=&gt;$img) {
    //$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
    echo '&lt;table style="float: left; border: 1px solid #EFEFEF; border-radius: 5px; padding: 5px; margin: 5px;"&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="'. $dir . $img .'" rel="example_group" &gt;&lt;img src="' . $dir . $img . '" alt="' . $img . '" id="'. "img_" . $i .'"/&gt;
    &lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="submit" class="ctrlDelete" value="" id="'. "del_" . $i .'"/&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;';
    $i++;
}
  echo "&lt;/div&gt;";
  ?&gt;&lt;/form&gt;
</code></pre>

<p>So, I need to make a method so that each button deletes its respective image and the form is posted back to self.</p>
