Recon Recon - 1 month ago 15
PHP Question

PHP Textbox writing to .md file

I'm creating a little application for myself to handle my .md file that contains my todo list etc.

<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);
}
?>


Whenever I enter a value into the textbox and click submit, the file doesn't update and stays the same. I have a feeling it might be that the submit button i s submitting the value of the blank file, but i there a work around for this? I need to be able to edit the file, not wipe it and start again.
Thanks!

Answer

Try this.

<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
    }

?>