2016-10-22 64 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