从表单变量创建可点击的链接

发布于 2024-12-08 19:39:41 字数 1367 浏览 1 评论 0原文

使用动态页面 1.生成文件夹中的文件列表 2. 制作该列表的选择框 3. 提供指向选定文件的可点击链接

步骤 1 和 2 的工作就像一个魅力,但我似乎无法传递该变量并使其成为可点击的链接。

这是我的代码:

<?php

// Step 3. Create clickable link from selection
if (isset($_POST['submit'])) {

$optionVal = $_POST[$file];
echo '<a href="'.$optionVal.'">Click to download: <strong>'.$optionVal.'</strong></a>';
} else {

// Step 1: Get file listing 
$show_path = 1;   # Show local path.
$show_dotdirs = 1;   # Show '.' and '..'.
$path = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/') + 1);

$dirs = array();
$files = array();

$dir = dir($path);
while ($entry = $dir->read()) {
    if (($entry != '.') and (substr($entry, -4) != '.php')) {
        if (is_dir($entry)) {
            if (($entry != '..') or $show_dotdirs){
                $dirs[] = $entry;
            }
        } else {
            $files[] = $entry;
        }
    }
}
$dir->close();
?>
<form action="pagelist.php" method="post">
<label>Select your lab: <select name="lab">
<?php
// Step 2: Make file listing in to selection box
sort($files);
foreach ($files as $file) {
    echo('<option value="'.$file.'">'.$file.'</option>');
}
?>
</select></label>
<input name="submit" type="submit" value="Go">

</form>

<?php } ?>

Working with a dynamic page that
1. generates a list of files in the folder
2. makes a selection box of that list
3. Provides a clickable link to selected file

Steps 1 and 2 working like a charm, but I just can't seem to pass that variable through and make it a clickable link.

Here's my code:

<?php

// Step 3. Create clickable link from selection
if (isset($_POST['submit'])) {

$optionVal = $_POST[$file];
echo '<a href="'.$optionVal.'">Click to download: <strong>'.$optionVal.'</strong></a>';
} else {

// Step 1: Get file listing 
$show_path = 1;   # Show local path.
$show_dotdirs = 1;   # Show '.' and '..'.
$path = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/') + 1);

$dirs = array();
$files = array();

$dir = dir($path);
while ($entry = $dir->read()) {
    if (($entry != '.') and (substr($entry, -4) != '.php')) {
        if (is_dir($entry)) {
            if (($entry != '..') or $show_dotdirs){
                $dirs[] = $entry;
            }
        } else {
            $files[] = $entry;
        }
    }
}
$dir->close();
?>
<form action="pagelist.php" method="post">
<label>Select your lab: <select name="lab">
<?php
// Step 2: Make file listing in to selection box
sort($files);
foreach ($files as $file) {
    echo('<option value="'.$file.'">'.$file.'</option>');
}
?>
</select></label>
<input name="submit" type="submit" value="Go">

</form>

<?php } ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-12-15 19:39:41

您应该将此行:

$optionVal = $_POST[$file];

更改为:

$optionVal = $_POST['lab'];

You should change this line:

$optionVal = $_POST[$file];

Into:

$optionVal = $_POST['lab'];
子栖 2024-12-15 19:39:41

$optionVal = $_POST[$file]; 是你的问题。这可能应该是 $optionVal = $_POST['lab'];

$optionVal = $_POST[$file]; is your problem. This should probably be $optionVal = $_POST['lab'];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文