December 23, 201015 yr Hello, Can anyone guide me to a tutorial on (or perhaps even tell me) how to make an extremely simple editable text box (not a rich text editor, just the box without word processing stuff) that will save the contents to a .txt file? I've searched Google but it keeps wanting me to take a look at rich text editors and PHP editing software, which isn't what I'm after. To clarify, I'd like a user to be able to write in a text box on a web page, and when they click Save, whatever they're written gets saved in to a .txt file. Thank you!
December 23, 201015 yr This is quite a simple example: <?php if (!empty($_POST['text'])) { $name = uniqid(); file_put_contents('/path/to/folder/' . $name . '.txt', $_POST['text']); } ?> <form action="" method="post"> <textarea name="text"></textarea> <input type="submit" name="submit" value="post" /> </form> This will add a new .txt file into the /path/to/folder/ directory and will give it a unique name. Edited December 23, 201015 yr by KieranA
December 29, 201015 yr Author Thanks for that, it worked nicely. I've changed it to always have the same filename though, which is what I'm after, and that works fine too. I'm trying to learn about classes, can someone help me change it a bit? I've written a class in "includes/blog.php" which goes like this: <?php class blog { public function postBlogPost() { if (!empty($_POST['text'])) { file_put_contents('includes/blog.txt', $_POST['text']); } } } ?> And so my page with the form on looks like this: ... <?php include 'includes/blog.php'; // include blog class $blog= new blog(); // create an object of the class blog ?> ... <form action="" method="post"> <textarea name="text"></textarea> <input type="submit" name="submit" value="post" /> </form> ... But it isn't working because I'm not sure how to call the function with the form. Can anyone tell me how it's done? Thank you!
December 29, 201015 yr <?php class blog { public function postBlogPost($contents='') { if (!empty($contents)) { file_put_contents('includes/blog.txt', $contents); } } } ?> <?php if(isset($_POST['submit'])) { include 'includes/blog.php'; // include blog class $blog = new blog(); // create an object of the class blog $blog->postBlogPost($_POST['text']); } ?>
Create an account or sign in to comment