Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help setting up extremely simple fwrite form

Featured Replies

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!

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 by KieranA

  • 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!

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.