简体   繁体   English

PHP表单动态输入

[英]PHP form dynamic inputs

Very new to php... bear with me! PHP的新手...忍受我!

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. 这将是我的Web主机服务器上文件夹(或“图库”)的动态视觉表示。 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. 每个div块都将包含该文件夹的第一张图片以及该文件夹的名称-标题文本和图片都应可单击,并充当提交按钮以继续到album.php页面,并通过传递该文件夹的名称GET方法。

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. 我遇到的第一个问题是尝试摆脱表单中的“提交”按钮,并用<a>标记中的相册缩略图和相册标题代替。 Apparently via javascript, and I managed to get this to work - kind of. 显然通过javascript,我设法使它正常工作。 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! 我已经将页面设置为具有多种输入形式的表单,但是现在每个单独的文件夹名称都在URL中传递(例如,mywebsite.com/album.php?name=album1&name=album2&name=album3),并且我绝对被卡住了!

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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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