简体   繁体   English

显示内容-在同一php页面中单击按钮

[英]Display content- click button within the same php page

I have successfully managed to read files via php from a directory and dynamically create a drop down menu where I can see the content of the files. 我已经成功地通过php从目录中读取了文件,并动态创建了一个下拉菜单,在其中可以看到文件的内容。 However, I did this via calling another php page. 但是,我通过调用另一个php页面来做到这一点。 Is there a way to display the content of the text files within the same page? 有没有办法在同一页面中显示文本文件的内容? I know that this can be achieved by Ajax but I can't find any examples. 我知道这可以通过Ajax实现,但是我找不到任何示例。

Thanks 谢谢

<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 function 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 function download.php函数

<html>
<body>
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=".$_GET['path']);
readfile($_GET['path']);
?>
</body>
</html>
$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 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 // basically the same 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) );
?>

So when you will select an option, it will load in your file. 因此,当您选择一个选项时,它将加载到您的文件中。

I hope, I correct understood what you wanted.. So no Iframe and $_GET[], 希望我正确理解了您的需求。.所以没有iframe和$ _GET [],

First output filenames as option in dropdown, on select pass file to ajax and it will return the content. 选择下拉列表中的第一个输出文件名作为选项,选择将文件传递给ajax,它将返回内容。 YOu can play around for more... 您可以玩更多...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM