简体   繁体   English

用PHP数组填充HTML表单下拉菜单

[英]Populating a HTML form drop down menu with a PHP array

I am trying to populate an HTML form's drop down list with the contents of the current directory. 我正在尝试使用当前目录的内容填充HTML表单的下拉列表。 I attempted to use PHP's scandir() function to output these files as an array and then feed them into the option's value tag within the HTML form. 我试图使用PHP的scandir()函数将这些文件输出为数组,然后将其输入HTML表单内的选项的value标记中。

I looked at various solutions available on SO and outside, but none seemed to work for me. 我查看了SO上和外部可用的各种解决方案,但似乎没有一种对我有用。

This is how my code looks like right now: 这是我的代码现在的样子:

<form action="" >
        <input type="submit" class="Links" value="Select Input">
        <select >               
            <?php 
                $dir="./inputfiles/"; 
                $filenames = scandir($dir); 
                foreach($filenames as $file){
                    echo '<option value="' . $filenames . '">' . $filenames . '</option>';
                    }
            ?>                      
        </select> 
</form> 

For now I'm getting absolutely no options in the drop down menu. 现在,我在下拉菜单中没有任何选择。

I am very new to PHP and would appreciate your feedback as to how to make this work. 我对PHP还是很陌生,非常感谢您对如何进行这项工作提供反馈。


Further Changes: 进一步的变化:

I tried the solutions given below, and none seemed to work. 我尝试了下面给出的解决方案,但似乎都没有用。 I changed the code to check whether the PHP script is able to output any variable or not in the html forms list. 我更改了代码,以检查PHP脚本是否能够在html表单列表中输出任何变量。

<form action="" >
        <input type="submit" class="Links" value="Select Input">
        <select >               
            <?php 
                  $someval = "Video";
                  echo '<option value="value"> ' . $someval .' </option>';
            ?>                      
        </select> 
</form>

It displays ' . 它显示'。 $someval .' $ someval。 instead of Video in the menu bar 而不是菜单栏中的视频

You have to use $file in the echo out not $filenames . 您必须在回显中使用$file而不是$filenames Like this: 像这样:

echo '<option value="' . $file . '">' . $file . '</option>';

See PHP foreach for more info. 有关更多信息,请参见PHP foreach

When iterating over the array, you need to use $file for concatenation, not $filenames : 遍历数组时,需要使用$file进行串联,而不是$filenames

echo '<option value="' . $file . '">' . $file . '</option>';

If inputfiles is present in the same directory, you should use the path: inputfiles/ 如果inputfiles出现在同一目录下,你应该使用的路径: inputfiles/

A couple of concerns with your script. 您的脚本有几个问题。

First, you need to output the correct variable within the options: 首先,您需要在选项中输出正确的变量:

echo '<option value="' . $file . '">' . $file . '</option>';

Second, you will likely need to add detection for . 其次,您可能需要为添加检测. and .. in your loop, as you will likely not want to output them. ..在您的循环中,因为您可能不想输出它们。 You might also want to exclude directories as well. 您可能还希望排除目录。

Third, you should probably make it clear exactly what directory you are trying to scan. 第三,您可能应该明确说明您要扫描的目录。 Try using a full file path. 尝试使用完整的文件路径。 If you need it relative to the currently executing file's directory you can do something like: 如果需要相对于当前正在执行的文件目录的文件,则可以执行以下操作:

$dir= __DIR__ . DIRECTORY_SEPARATOR . 'inputfiles'; // no need for trailing slash

Fourth, you might want to consider working with something like DirectoryIterator to give you some more convenient methods to do what you are looking to do (ie verifying it's an actual file before listing it). 第四,您可能希望考虑使用DirectoryIterator之类的工具,为您提供一些更方便的方法来完成您想要做的事情(即在列出文件之前先确认它是实际文件)。 So something like this: 所以像这样:

$dir= __DIR__ . DIRECTORY_SEPARATOR . 'inputfiles';
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $fileinfo) {
    if($fileinfo->isFile()) {
        echo '<option value="' . $fileinfo->getFilename() . '">' . $fileinfo->getFilename() . '</option>';
    }
}

In this trivial example, it you won't necessarily gain a lot (in terms of saving extra code) by using DirectoryIterator, but I certainly like to point out it's usage when I can, because if you needed to start doing things like showing file size, file permissions, file modification times, etc. It is much easier to do it using the DirectoryIterator class (or RecursiveDirectoryIterator class if you need to recursive) than using traditional PHP's file system functions. 在这个简单的示例中,使用DirectoryIterator不一定会带来很多好处(但是节省了额外的代码),但是我当然想指出它的用法,因为如果您需要开始执行类似显示文件的操作大小,文件许可权,文件修改时间等。使用DirectoryIterator类(如果需要递归,则使用RecursiveDirectoryIterator类)比使用传统的PHP文件系统功能要容易得多。

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

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