如何通过使用PHP单击链接来获取文本文件中的数据

如何通过使用PHP单击链接来获取文本文件中的数据,php,html,Php,Html,下面是我的代码,它获取文本文件home.txt的值 <script type="text/javascript"> function show_content() { document.getElementByID('hidden_content').style.display = 'active'; } </script> <title>CMS</title> </head> <?php $myFile = "h

下面是我的代码,它获取文本文件home.txt的值

<script type="text/javascript">
function show_content() {
    document.getElementByID('hidden_content').style.display = 'active';
}
</script>
    <title>CMS</title>
</head>
<?php
$myFile = "home.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo '<div id="hidden_content" style="display:none">'.$theData.'</div>';
?>
<a href="cms.php" onclick="show_content()">Show it!</a>

函数show_content(){
document.getElementByID('hidden_content')。style.display='active';
}
CMS
当我单击“显示”链接时,此代码未获取我的文件文本。我没有得到任何错误。请帮我试试这个:

<!DOCTYPE html>
<html>
    <head>
    <title>CMS</title>
    <script type="text/javascript">
        function show_content(){
          let lines = window.document.getElementsByClassName('file-line');
          for (let i of lines) {
            i.style.display = 'block';
          }
        }
    </script>
    </head>
    <body>
      <?php
          $fh = fopen("home.txt", "r");
          while (!feof($fh)) {
            $line = fgets($fh);
            echo '<div class="file-line" style="display:none;">'.$line.'</div>';
          }
          fclose($fh);
       ?>

       <a href="#" onclick="show_content()">Show it!</a>

     </body>
</html>

CMS
函数show_content(){
让line=window.document.getElementsByCassName('file-line');
for(让我看一行){
i、 style.display='block';
}
}

您遇到了什么错误?或者发生了什么?当我单击“显示它”链接时,它不显示home.text文件值。如果存在错误,您可以更新我的代码以删除错误。回答问题,并根据评论予以澄清。评论可以删除,并且不属于问答的一部分。显示您的研究以及任何错误或消息。没有人会为您修复代码,所以您需要自己调试。@fameknifest查看并获取该文件。@fameknifest我将删除div中的id您的回显,因为您不应该有超过一个具有该id的元素,使用类instead@Fame如果这不起作用,那么我猜您的文件不在正确的位置(文件路径不正确)。您可以使用var_dump($line);或者var_dump($fh),以确保您实际读取了该文件。听我说,当我点击html时,同一页面上的文本文件必须打开这里是我的代码$myFile=“file.txt”$fh=fopen($myFile,'r')$theData=fread($fh,filesize($myFile));fclose($fh);回显“.$theData.”;HTML链接:Javascript:function show_content(){document.getElementByID('hidden_content').style.display='block';}如果有原因,请查找其中的任何内容