2013-10-17 185 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

相关问题