2016-10-22 198 views
0

我创建一个小应用程序,为自己来处理包含我的待办事项列表等我.MD文件PHP文本框写入.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); 
    } 
?> 

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

回答

3

试试这个。

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

?> 
+0

这并不完全达到我想要做的,我需要编辑整个文档,而不是添加一个新行。 – Recon

+0

您是否需要通过文本框编辑整个文档? – Soliyappan

+0

是的,这就是目标。 – Recon

1

试试这个。

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

不起作用,创建相同的结果。 – Recon

+0

您是否尝试从该位代码中删除注释'$ myFileContents。=“\ n”;'? – harryparkdotio

+0

这不会产生我需要的效果。我需要编辑和更新.md文件上的文本。不要创建新行。 – Recon