テキストファイルに内容を書き込み、テキストファイルから内容を取得するスクリプト

HTML(index.html)

javascript

<script>            
$(document).ready(() => {
    $.get("data.txt", (result) => {
        $('p').html(result.replace(/\n/g, "
")); }); $("p").html(result) }); $('form').on('submit', function(e) { let comment = $('textarea').val(); e.preventDefault(); $.ajax({ url: "server_script.php", type: "POST", data: { comment: comment }, success: (result) => { $('p').html(result.replace(/\n/g, "
")); $('textarea').val(""); } }); }); </script>

PHP(server_script.php)

<? php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $comment = $_POST["comment"]."\n";
        file_put_contents("data.txt", $comment, FILE_APPEND);
        $comment = file_get_contents("data.txt");
        echo $comment;
    }
?>