How to: Your own online Notpad

Get the how-to on creating your own online Notpad for your own website. You can use it as a member notpad, to-do list, admin notpad, or anything else. And best of all it uses text-files.

Now first-off:

1. Create a blank .txt file named notes.txt then upload it to your server.
2. Open the file you want to put your 'Notepad' into.
3. Find where you would like to place the code.
4. Add this code there...

CODE
<strong>Notes ::</strong>

            <?php
        
if($_POST['update']) {
        
$newmsg stripslashes($_POST['message']);
        
$file fopen("notes.txt","r+");
        
ftruncate($file,0);
        
$writedate fwrite($file,$newmsg);
        
fclose($file);
        }
        
$messages fopen("notes.txt","r+");
        
$message fread($messages,100000);
        
fclose($messages);
        
?>

        <form action="<? $php_self ?>" method="post">
        <textarea name="message" rows="4" cols="40"><?=$message?>
        </textarea>
        
<input type="submit" name="update" value="Update" />
</form>



Now what that code does is display an description box like the example below and after clicking Update, all the information in the textbox is added to notes.txt

REMEMBER: Now always remember that when writing to a file in php, you need to CHMODD that file which in our case is notes.txt to 755 or 777 depending on your server. Experiment if that dosn't work for you.


Now some of you might be asking, "Why did he use stripslashes and what is that"

Well stripslashes are to get rid of those nasty's.
Example being:

Without:
Jim's dog ran away.

With:
Jim's dog ran away.


Okay so now to explain the PHP part of this tutorial.


CODE
if($_POST['update']) {

This checks to see if anybody pressed Update.



CODE
$newmsg = stripslashes($_POST['message']);

This prevents the 's from happening. So it gets what text whoever typed into the textbox and fixes the nasty 's.



CODE
$file = fopen("notes.txt","r+");

This opens notes.txt and prepares it for what text whoever has put into the textarea to be added.
Also the r+ means that it makes the file (notes.txt) open for reading and writing; place the file pointer at the beginning of the file.



CODE
ftruncate($file,0);

The ftruncate() function causes the regular file referenced by fildes to have a size of length bytes.
The truncate() function causes the regular file named by path to have a size of length bytes.



CODE
$writedate = fwrite($file,$newmsg);

This get the $file which is notes.txt and the message whoever typed in the textbox and writes it to notes.txt.



CODE
fclose($file); }

This close's the connection of the opening of notes.txt or $file which is told to be notes.txt



CODE
$messages = fopen("notes.txt","r+");

Opens the file to doubble check ;) and the r+ makes (notes.txt) open for reading and writing; place the file pointer at the beginning of the file.



CODE
fclose($messages);


This closes the messages or the text in the textarea connection.


Thats a total explination of how to make an notepad/textarea on your website!
If you have any questions, please feel free to ask! We would be glad to help you.

Sunday May 27, 2007 | http://www.totaldream.org/article/23-how_to:_your_own_online_notpad.html