cr80expert5 Posted February 16, 2011 Share Posted February 16, 2011 From what I have so far is that I am reading a text file into a textbox. Now I want to update/append new information to my file(the information may contain HTML/PHP code, text etc.) after I click the save button so the contents can be read on a separate page called "edit.php". Any suggestions? (My first time using PHP on a higher level) The following is written in my editcontent1.php(I'm seperating each content block on my edit.php page into several editcontent(n).php files where n is = 1 to infinity) Here is before the html tags: <?php if ( isset ($_POST ['content']) ) { file_put_contents ('content1.txt', $_POST ['content'] ); } ?> Here is before the html tags: <form action = "edit.php" method="post"> <textarea name="content" cols="" rows="" wrap="virtual" class="textarea1"> <?php include 'content1.txt'; ?> </textarea> <br /> <input type="submit" value="Save"> </form> Link to comment https://forums.phpfreaks.com/topic/227933-reading-file-in-a-textbox-and-replacing-the-information-within-it-after-saving/ Share on other sites More sharing options...
petroz Posted February 17, 2011 Share Posted February 17, 2011 Something like this might help... <?php $file = 'mytextfile.txt'; $data = $_POST['content']; if($_SERVER['REQUEST_METHOD'] == "POST") { //update the text file $fp = fopen($file, 'w+'); fwrite($fp, $data); fclose($fp); ?> <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <textarea name="content" cols="" rows="" wrap="virtual" class="textarea1"> <?php include $file; ?> </textarea> <br /> <input type="submit" value="Save"> </form> <? } else { ?> <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <textarea name="content" cols="" rows="" wrap="virtual" class="textarea1"> <?php include $file; ?> </textarea> <br /> <input type="submit" value="Save"> </form> <? } Link to comment https://forums.phpfreaks.com/topic/227933-reading-file-in-a-textbox-and-replacing-the-information-within-it-after-saving/#findComment-1175402 Share on other sites More sharing options...
jcbones Posted February 17, 2011 Share Posted February 17, 2011 I see nothing wrong with the OP's code, just a little more sanitation. Although, you may want to re-think how you are going about this. Such as: 1. Why do you need to have a form to create php code? You could just create/append/edit it in an editor, then ftp it to your site. 2. Why not use a database? Easier to maintain, and you wouldn't have ~infinity files. Link to comment https://forums.phpfreaks.com/topic/227933-reading-file-in-a-textbox-and-replacing-the-information-within-it-after-saving/#findComment-1175410 Share on other sites More sharing options...
petroz Posted February 17, 2011 Share Posted February 17, 2011 Totally agree with you on the database.... but I think OP needs to get a more clear understanding for forms and how PHP works with them... Link to comment https://forums.phpfreaks.com/topic/227933-reading-file-in-a-textbox-and-replacing-the-information-within-it-after-saving/#findComment-1175413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.