如何使用 jscript 和 php 更改 php 读取文件。新 Q 如何阻止它影响 header

How to change a php read file using jscript and php. New Q how to stop it effecting the header

请注意,这仍然没有按预期工作,它没有将信息放入页面,而是重定向到一个新页面

更新 新问题是我不希望用户看到大量的 header,这是我使用此读取文件系统的原因,有什么方法可以在更改 contentFile 时避免大量的 header?提前致谢 这是否可以从 .htaccess 文件中实现,或者这不是一个选项

提前致谢

我的问题是,是否可以使用 ajax、jscript 或 html 更改读取文件。因此,如果用户单击 php 中的按钮,它会更改读取的文件

例子

//display content    
<?php
    $contentFile = "content.php";
    readfile( $contentFile );
?>

重定向

<script type="text/javascript">
   document.getElementById("myButton").onclick = function () 
{
                       location.href = "./content2.php";
};
</script>

    <p>TO POPULATE THE TABLES CLICK THE BUTTON: 
           <button id="myButton2" class="float-left submit-button" >changecontent</button></p>
                        </br>
   //

任何帮助都会很棒

更新 下面的工作代码

  <section id="content" class="content">
    <button id="newButton" class="float-left submit-button" >changecontent</button></p>
        </br>
    <?php
    if(isset($_GET['filename'])){
        $contentFile = $_GET['filename'];
    }else{
        $contentFile = 'content.';
    }
    readfile( $contentFile.'php' );
    ?>

    <script type="text/javascript">
        document.getElementById("newButton").onclick = function()
    {
        location.href= "test.php?filename=content2.";
    };  
    </script>
</section>
Try this logic
 PHP code...
 if(isset($_GET['filename'])){
     $contentFile = $_GET['filename'];
 }else{
     $contentFile = 'content1';
 }
 readfile( $contentFile.'.php' );

<script type="text/javascript">
   document.getElementById("myButton").onclick = function () 
{
      location.href = "change_file.php?filename=content2";
};
</script>

您分配的 ID 有误,请使用下面的 javascript。

<script type="text/javascript">
   document.getElementById("myButton2").onclick = function () 
{
                       location.href = "./content2.php";
};
</script>

希望对您有所帮助