Anil Kumar Nelakuditi Anil Kumar Nelakuditi - 29 days ago 5
HTML Question

How to display Text file on clicking button in html

hi i have four text files and i want to display it on browser using buttons i want to create four button when click on 1st button first text should open. Please help me out from this i'm new to html and Php.

Answer

To do this you need to read about php file functions.

Refer this link - http://php.net/manual/en/function.file.php

Try below code:

<h2>Click</h2>
<form action="" method="post">
    <button name="click" class="click">Click me!</button>
</form>

    <?php
    if(isset($_POST['click']))
    {
        $filename = "test.txt";
        $before_editing = file_get_contents($filename);

    ?>
<!--this is for display in table-->
<table border="1"><tr><td><?php echo "Content of the file " . $filename . " before editing: " . $before_editing . "<br>"; ?></td></tr></table>
<!-- this is for display in single-->
    <?php
        echo "Content of the file " . $filename . " before editing: " . $before_editing . "<br>";
        file_put_contents($filename, "test test test test test");
        $after_editing = file_get_contents($filename);
        echo "Content of the file " . $filename . " after editing: " . $after_editing . "<br>";
    }
    ?>

You can replace your file name to "test.txt" as i am just giving example to you.

Comments