asp.net consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

IT Services And Support

Email : ren@techsolus.co.uk
Mobile Phone : 0788 68 41 411
Answerphone : 01204 469683
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull


Get connected and Online Tottington
remote management Makerfield code and design Aspull

Auto Save A Textarea - Javascript (And Ajax) - 21/05/2014

This is another one of those things that is made VERY complex on the internet. Here's my simple version.

*The page containing the text area you wish to auto save*
 
<!DOCTYPE html>
<html>
<head>
 
<title>
 Report
</title>
 
<script>
 
function mysave()
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
 
xmlhttp.open("POST","savepage.aspx", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("text=" + document.getElementById("mybox").value);
}
 
</script>
 
</head>
<body onload = "setInterval(function(){mysave()},5000);" >
<form runat="server">
 
<asp:textbox id = "mybox" textmode = "multiline" width = "400px" height = "400px" runat = "server" />
 
</form>
</body>
</html>
 
Now, note the javascript in bold.
 1. We create the variabe "var".
 2. We make var a new XMLHttpRequest() 
 3. We open for "POST", use post rather than "GET" because you might have a very large amount of data in your text area.
 4. We add the header. I don't know why, if you want to know look elsewhere!
 5. We send the request. Basically the javascript in your browser asks for the webpage stated in the xmlhttp.open. When it asks for it it also sends the POST data found in "mybox", which will be the name and id of your text area. 
 
Unlike many Ajax calls we are NOT expecting a response. We just request the page with the contents of the text area. 
 
In the instance above we request the page "savepage.aspx". If like my you're an ASP.Net programmer the page can collect the text area details thusly
 
dim textareatosave as string
textareadatatosave = request("text")
 
Then save it. Save it to a file or database, that's up to you. 

Post A Comment

Name Comment
programming services asp.net specialist
Valid XHTML 1.0 Transitional
Admin
GD