1

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 } ?>

2 Answers 2

1

You should change this line:

$optionVal = $_POST[$file];

Into:

$optionVal = $_POST['lab'];
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.