简体   繁体   中英

PHP form dynamic inputs

Very new to php... bear with me!

I'm creating a photography website and am stuck on a particular page. Ultimately I need the page to be a form with a dynamic number of inputs. It will be a dynamic visual representation of folders (or "galleries") on my web host server. Each div block will contain the first image from the folder along with the name of the folder - the heading text and image should both be clickable and act as a submit button to continue to the album.php page, passing the name of the folder via a GET method.

The first problem I had was trying to get rid of the submit button from a form, and replace with my album thumbnail and album title in an <a> tag. Apparently via javascript, and I managed to get this to work - kind of. I've gotten the page to a state where there is a form, with multiple inputs, but every single folder name is now being passed in the URL (eg mywebsite.com/album.php?name=album1&name=album2&name=album3) and I'm absolutely stuck!

The code is included, so if someone could have a look over and offer some guidance or point me in the right direction (and offer any other newbie tips!) it would be much appreciated.

Cheers Lee

<form id="album" method="get" action="album.php">

<?php
    $basepath = 'images/portfolios/public/';
    $results = scandir($basepath);

    foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;

            if (is_dir($basepath . '/' . $result)) {
                //create new gallery for each folder
              ?>

                 <div class="gallery">

                    <?php

                       //get count of images in folder
                   $i = 0; 
                   $path = 'images/portfolios/public/' . $result;

                   if ($handle = opendir($path)) {
                      while (($file = readdir($handle)) !== false){
                          if (!in_array($file, array('.', '..')) && !is_dir($path.$file)) 
                          $i++;
                      }
                       }

               //create array, and choose first file for thumbnail
                   $files = array();   
               $dir = opendir($path);   
               while(($file = readdir($dir)) !== false)   
               {   
                  if($file !== '.' && $file !== '..' && !is_dir($file))   
                  {$files[] = $file;} 
               }   
               closedir($dir);   
               sort($files);   

               //form input - album name for next page
               echo '<input type="hidden" name="name" id="name" value="'.$result.'" />';

               //the headline and image link for gallery
                   echo '<a href="javascript: submitform()">' . $result . ' - ('.$i.' images)<br>
                   <img src="'.$path.'/'.$files[0].'" />
                   </a>';

                ?>

             </div> <!-- end gallery -->


                 <?php }
          }
        ?>

</form>
<script type="text/javascript">
function submitform(){document.forms["album"].submit();}
</script>

change your code from:

echo '<a href="javascript: submitform()">' . $result . ' - ('.$i.' images)<br>
          <img src="'.$path.'/'.$files[0].'" />
      </a>';

to this code:

echo '<a href="admin.php?name='.$result.'">' . $result . ' - ('.$i.' images)<br>
          <img src="'.$path.'/'.$files[0].'" />
      </a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM