2011-10-10 187 views
1

與動態網頁, 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 } ?> 

回答

1

你應該改變這一行:

$optionVal = $_POST[$file]; 

分爲:

$optionVal = $_POST['lab']; 
+0

如此明顯。謝謝。 –

1

$optionVal = $_POST[$file];是你的問題。這應該可能是$optionVal = $_POST['lab'];

+0

當然!謝謝。 –