Log in

View Full Version : Saving Textarea



Trav
07-04-2008, 03:47 AM
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).

Nile
07-04-2008, 04:25 AM
Here, this should work, its using PHP:


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

rangana
07-04-2008, 04:44 AM
JS:


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

codeexploiter
07-04-2008, 06:16 AM
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.

rangana
07-04-2008, 06:41 AM
Done. Highlighted is added.