PHP 写入 .md 文件的文本框

PHP Textbox writing to .md file

我正在为自己创建一个小应用程序来处理我的 .md 文件,其中包含我的待办事项列表等。

<html>
    <body>
        <form name="form" method="post">
            <input type="text" name="text_box" size="50" value="<?php include("me.md"); ?>"/>
            <input type="submit" id="search-submit" value="submit" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST['text_box'])) { //only do file operations when appropriate
        $a = $_POST['text_box'];
        $myFile = "me.md";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $a);
        fclose($fh);
    }
?>

每当我在文本框中输入一个值并单击提交时,文件不会更新并保持不变。我有一种感觉,提交按钮可能正在提交空白文件的值,但我有解决这个问题的方法吗?我需要能够编辑文件,而不是擦除它并重新开始。 谢谢!

试试这个。

<html>
    <body>
        <form name="form" method="post">
            <!--<input type="text" name="text_box" size="50" value="<?php //echo file_get_contents('me.md'); ?>"/>-->
            <textarea name="text_box" rows="10" cols="30"><?php echo file_get_contents('me.md'); ?></textarea>
            <input type="submit" id="search-submit" value="submit" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST['text_box'])) { //only do file operations when appropriate
        $a = $_POST['text_box'];
        $myFile = "me.md";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $a);
        fclose($fh);
        echo "<meta http-equiv='refresh' content='0'>"; //Refresh the same page
    }

?>

试试这个。

<html>
    <body>
        <form name="form" method="post" action="">
            <input type="text" name="text_box" size="50" value="<?php echo file_get_contents('me.md'); ?>"/>
            <input type="submit" id="search-submit" value="submit" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST['text_box'])) {
        file_put_contents("me.md", $_POST['text_box']);
        header("Refresh:0");
    }
?>