从表单变量创建可点击的链接
使用动态页面 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将此行:
更改为:
You should change this line:
Into:
$optionVal = $_POST[$file];
是你的问题。这可能应该是$optionVal = $_POST['lab'];
$optionVal = $_POST[$file];
is your problem. This should probably be$optionVal = $_POST['lab'];