【问题标题】:Populate Dropdown list appears empty填充下拉列表显示为空
【发布时间】:2015-03-09 00:27:40
【问题描述】:

感谢大家一直给予我的帮助。我现在的下拉框似乎在改变,但它是空白的。我正在使用的当前编码编辑如下:

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select> 
<?php 
$directory = realpath(dirname(FILE)) . '/../users/' . $_SESSION['username']; 

$files = scandir( $directory ) ;

foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
    echo "<option>" . $file . "</option>";
}
}
?>
</select>

当我检查页面上的空下拉元素时,我得到了这个:

警告:scandir(/home/revo/public_html/evo/../users/Addiction) [function.scandir]: 无法打开目录:/home/revo/public_html/evo/codesaveindex 中没有这样的文件或目录.php 在第 117 行

警告:scandir() [function.scandir]: (errno 2): 第 117 行 /home/revo/public_html/evo/codesaveindex.php 中没有这样的文件或目录

警告:在第 119 行的 /home/revo/public_html/evo/codesaveindex.php 中为 foreach() 提供了无效参数

【问题讨论】:

    标签: php


    【解决方案1】:

    你的问题是你输出了一个&lt;select&gt;标签,然后你开始构建一个带有连接的php变量$Selection(这是错误的,因为你从未在连接之前初始化变量)。

    看看这是否有效:

    <input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
    <?php
    
    // match all files that have .txt extension
    $file_matcher = realpath(dirname(__FILE__)) . '/../users/' . $_SESSION['username'] . '/*.{txt}';
    
    //Initialize variable $Selection
    $Selection = '<select size="1" name="CodeList" id="CodeList" onchange="CodeChange();"><option value="0">(Add New Code)</option>';
    
    foreach( glob($file_matcher, GLOB_BRACE) as $file ) {
        $file_name = basename($file);
        //Concatenate variable $Selection
        $Selection .= "<option value='$file'>$file_name</option>\n";
    }
    
    //Finish HTML source concatenation on $Selection
    $Selection .= '</select>';
    
    //Output the HTML
    echo $Selection;
    ?>
    

    【讨论】:

    • 我错过了选择标签,感谢您的更新。但是,当我将其放入并尝试加载页面时,它会返回:Parse error: syntax error, unexpected T_STRING in /home/revo/public_html/evo/codesaveindex.php on line 145
    • 我不太确定我写对了,谢谢。它按原样保存文件,但是页面加载时的下拉菜单仍然是空白的并且没有填充。我不确定在运行保存脚本后是否重定向回错误的页面或发生了其他事情。当我检查页面上的元素时,它会引发错误: Uncaught SyntaxError: Unexpected token ILLEGAL , codesaveindex.php: 21
    • 那一行比上一个错误多很多,你能输出原始的php源吗?
    • 修复了所有这些问题并修复了重定向,没有看似错误,只是没有填充下拉列表。
    【解决方案2】:

    我认为这应该可以将 DIR 替换为您想要的路径

    <select> 
    <?php
    
    $directory = __DIR__;
    
    $files = scandir( $directory ) ;
    
    foreach( $files as $file )
    {
        if ( ! is_dir( $file ) )
        {
            echo "<option>" . $file . "</option>";
        }
    }
    ?>
    
    </select>
    

    【讨论】:

    • 谢谢你。这改变了下拉列表,但它显示为空白,我不确定我是否正在编写目录以及其他所有内容。
    • 当检查它返回的下拉元素时:警告:scandir(/home/revo/public_html/evo/../users/Addiction) [function.scandir]: failed to open dir: No such第 117 行 /home/revo/public_html/evo/codesaveindex.php 中的文件或目录警告:scandir() [function.scandir]: (errno 2): /home/revo/public_html/evo/ 中没有这样的文件或目录第 117 行的codesaveindex.php 警告:在第 119 行的 /home/revo/public_html/evo/codesaveindex.php 中为 foreach() 提供的参数无效
    • 你必须确保你有一个有效的路径,你可以实现一个检查目录是否存在并且不为空。
    【解决方案3】:

    你的目录路径有点复杂。

    $file_matcher = realpath(dirname(__FILE__)) . '/../users/' . $_SESSION['username'] . '/*.{txt}';
    

    如果目录无效,您将找不到任何文件并且不会有填充选择。

    只是为了测试,尝试一个简单的目录 - 没有任何 vars - 看看它是否有效。我敢打赌,在 Niloct 的帮助下,它会的。为 Niloct +1。

    【讨论】:

    • 感谢您的帮助 我感觉它可能在目录中 我只是不确定如何编写它以找到我需要的正确目录,它应该是 users/$_SESSION[' username'] 我不断收到语法或解析错误。
    • 先别担心。分而治之。首先,让它用任何文件名填充下拉框。然后,接受 Niloct 的回答并发布一个新问题以寻求路径导航方面的帮助。
    • 知道了。就这样做。我只使用他在那里的 Dir 命令让它弹出。将发布另一个问题并接受他的关于目录文件路径。谢谢!
    • 祝您项目顺利。 愿道路升起与你相遇……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2015-01-20
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多