2013-10-17 186 views
2

我已經成功設法通過php從目錄中讀取文件,並動態創建一個下拉菜單,我可以在其中查看文件的內容。不過,我通過調用另一個PHP頁面來完成此操作。有沒有辦法在同一頁面中顯示文本文件的內容?我知道這可以通過Ajax來實現,但我找不到任何示例。顯示內容 - 點擊同一個php頁面內的按鈕

感謝

<html> 
<head> 
<link rel="icon" type="image/png" href="logo.png"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript"> 
function populateIframe(id,path) 
{ 
    var ifrm = document.getElementById(id); 
    ifrm.src = "download.php?path="+path; 
} 
</script> 
<body> 
<?php 
    //"file_to_be_displayed" is the name of the dropdown 
    $selectedfile = @$_POST['file_to_be_displayed']; 
    $path ="$selectedfile"; 
    $content = file($selectedfile); 
    $data = implode("<br>",$content); 
    echo $data; 
?> 
<?php 
$dirname = "<path>"; 
$dir = opendir($dirname); 
echo '<form name="displayfile" action="print_output_file.php" method="POST" target="_blank">'; 
echo '<select name="file_to_be_displayed">'; 
echo '<option value="">Select File</option>'; 
while(false != ($file = readdir($dir))) 
     { 
     if(($file != ".") && ($file != ".."))    
     { 
     echo "<option value=".$file.">$file</option>"; 
     } 
     } 
       echo '</select>'; 
echo '<input type="submit" value="display content of file"/>'; 
echo '</form>'; 
?> 
</head> 
</form> 
<iframe id="frame1" style="display:none"></iframe> 
<a href="javascript:populateIframe('frame1','<?php echo $path; ?>')">download</a> 
</body> 
</html> 

print_output_file.php功能

<html> 
<body> 
<?php 
$myFile =$_POST['file_to_be_displayed']; 
$myFile1 ="http://<file path>/"."$myFile"; 
$lines = file("$myFile1"); 
$fileString = file_get_contents("$myFile1"); 
echo nl2br(htmlspecialchars($fileString)); 
echo '<br>'; 
?> 
</body> 
</html> 

的download.php功能

<html> 
<body> 
<?php 
header("Content-type: text/plain"); 
header("Content-Disposition: attachment; filename=".$_GET['path']); 
readfile($_GET['path']); 
?> 
</body> 
</html> 
+0

'download.php'是否有效?你有沒有搜索AJAX? – putvande

+0

嗨@putvande download.php目前無法正常工作,但目前我並不關心它。我已經搜索了AJAX,並在網上找到了一些例子,但不知道如何使用php中的'$ myFile'變量來指示AJAX顯示特定文件 – Pantelis

回答

1
$dirname = "<path>"; 
$dir = opendir($dirname); 
echo '<form action="" method="" target="">'; 
echo '<select id="select_file">'; 
echo '<option value="">Select File</option>'; 
while(false != ($file = readdir($dir))) 
     { 
     if(($file != ".") && ($file != ".."))    
     { 
     echo "<option value=".$file.">$file</option>"; 
     } 
     } 
       echo '</select>'; 
echo '</form>'; 

JS

('#select_file').on('change', function() { 
    var file_to_be_displayed = 'file_to_be_displayed='+$(this).val(); 
    $.ajax({ 
    type:'POST', 
    url: "print_output_file.php", 
    data:file_to_be_displayed, 
    success:function(data){ 
     $('#id_where_to_show').html(data); 
    } 
    }); 
}); 

PHP FILE_TO_BE DISPLAYED //基本相同

<?php 
$myFile =$_POST['file_to_be_displayed']; 
$myFile1 ="http://<file path>/"."$myFile"; 
$lines = file("$myFile1"); 
$fileString = file_get_contents("$myFile1"); 
echo nl2br(htmlspecialchars($fileString)); 
?> 

所以當你選擇一個選項,它會在你的文件中加載。

我希望,我理解正確,你想要的東西。所以沒有iframe和$ _GET [],

首頁輸出文件名在下拉選項,在選擇傳文件,AJAX,它會返回內容。 YOu可以玩到更多...

+0

Hi @Froxz框架和Get是下載功能目前工作。我已經嘗試了上述解決方案,但在選擇文件時它不起作用。我想仍然有按鈕,一旦我點擊它,然後JavaScript腳本應該顯示相關內容 – Pantelis

+0

你有什麼錯誤???爲什麼它不工作? ('#'選擇文件')。on('change',function(){'也許我錯過了某些東西... – Froxz

+0

沒有具體的錯誤,一旦我選擇文件什麼都不會發生。在這樣的代碼: <腳本類型= 「文本/ JavaScript的」> ('#select_file')。on('change',function(){ var file_to_be_displayed ='file_to_be_displayed ='+ $(this).val(); $ .ajax({type:'POST', url :「print_output_file.php」, data:file_to_be_displayed, 成功:function(data){('#id_where_to_show')。html(data); } }); }); – Pantelis

相關問題