Results 1 to 5 of 5

Thread: Saving Textarea

  1. #1
    Join Date
    Jul 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Saving Textarea

    Does anybody have any idea how to make a textarea where after typing in it you can click like a Save button, it saves it, and next time you come back it's still there? I've found a few, but to test them I go to task manger and end them, and when I go to them it didn't save. Any ideas?

    Also, it looks something like this...
    http://calle.ioslo.net/testing/textarea-save/

    (except with the save button, and this one didn't work).

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here, this should work, its using PHP:
    PHP Code:
    <?php
    if(isset($_POST['submit'])){
    $fh fopen('save.txt','w');
    $writeData $_POST['text'];
    fwrite($fh$writeData);
    fclose($fh);
    }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <textarea name="text"><?php echo file_get_contents('save.txt'); ?></textarea><br><input type="submit" value="Save" name="submit"><br>
    </form>
    A file with the name save.txt will be created when you run this script.
    Last edited by Nile; 07-04-2008 at 04:54 AM.
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    JS:
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    document.getElementById('save').onclick=function()
    {var el=document.getElementById('editArea'); // Id of element
    cookieSave('editArea',el.value);
    }
    document.getElementById('editArea').value=cookieLoad('editArea');
    }
    function cookieSave(name, k)
    {document.cookie = name + "=" + escape(k);}
    function cookieLoad(name)
    {
    	var search = name + "=";
    	if (document.cookie.length > 0)
    	{
    		offset = document.cookie.indexOf(search);
    		if (offset != -1) {
    			offset += search.length;
    			end = document.cookie.indexOf(";", offset);
    			if (end == -1) {
    				end = document.cookie.length;
    			}
    			return unescape(document.cookie.substring(offset, end));
    		}
    	}return '';
    }
    </script>
    <form id="editForm">
    <textarea id="editArea" style="width:500;height:200px;border:1px solid #999;"></textarea>
    <br>
    <input type="button" value="Save Cookie!" id="save">
    </form>
    Last edited by rangana; 07-04-2008 at 06:41 AM. Reason: Modify code
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I think the above script can be improved a bit more. It shows undefined in the textarea if the cookie doesn't exists, which I think should be solved. Hope you'll change the code to address this issue.

    As you know if you go for a cookie based approach there are few problems like at any time the user can remove the cookie(s) as well as the user browser might have disabled cookies in it.

    if you are looking for a more stable storage it is better if you proceed to server-side tools like PHP, ASP, etc.

  5. #5
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Done. Highlighted is added.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •