Solved

localstorage html forms

Posted on 2014-02-16
9
366 Views
Last Modified: 2014-02-17
the following code allows me to save the form offline and retrieve it successfully,
but i might have more than one form to save .. what can i do in this case ... thx

here the html code and js


<!DOCTYPE html>
<html manifest="offline.manifest">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Offline test page</title>
<link rel="stylesheet" type="text/css" media="all" href="styles.css" />
</head>
<body>

<p id="status">Online</p>

<form id="mainform" action="index.html" method="post">
<fieldset>
<legend>Update data</legend>

<label>Roxy item 1: <input type="text" id="data1" name="data1" value="" /></label>
<label>Foxy item 2: <input type="text" id="data2" name="data2" value="" /></label>
<label>Galops item 3: <input type="text" id="data3" name="data3" value="" /></label>

<button type="submit">Save Data</button>

</fieldset>
</form>

<script type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript" src="offline.js"></script>

</body>
</html>


--------------------------------------------------------------------------------------------


jQuery(document).ready(function($) {
	$("#mainform").submit(lib.Save);
	if (window.localStorage) {
		lib.Net.ChangeStatus();
		$(window).bind('online offline', lib.Net.ChangeStatus);
	}
	lib.Load();
});
var lib = lib || {};
lib.Net = function() {
	var online = true;
	function Online() { return navigator.onLine; }
	function ChangeStatus() {
		if (online != Online()) {
			online = Online();
			var s = $("#status");
			s.text(online ? "Online" : "Offline");
			if (online) s.removeClass("offline");
			else s.addClass("offline");
		}
	}
	return {
		Online: Online,
		ChangeStatus: ChangeStatus
	};

}();
lib.Save = function(e) {
	e.preventDefault();
	if (lib.Net.Online() || !window.localStorage) {
	alert("Data has been saved online.!)");
	}
	else {
		$("#mainform input").each(function(i) {
		window.localStorage.setItem(this.id, this.value);
		});
		alert("Data has been saved offline.");
		}

};
lib.Load = function() {
	if (lib.Net.Online() || !window.localStorage) {
		alert("Currently online:\ndata could be loaded from server.");
	}
	else {
		$("#mainform input").each(function(i) {
			this.value = window.localStorage.getItem(this.id);
		});
		alert("Data has been loaded from offline store.");
	}
};

Open in new window

0
Comment
Question by:mokaid83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39864192
How about


localStorage["form1"] = $("#form1").serialize();
localStorage["form2"] = $("#form2").serialize();
0
 

Author Comment

by:mokaid83
ID: 39864267
the case is that the user might fill more than time the same form and you are suggesting i have to create multiple instances of the same form , am i mistaken ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39864309
Yes you are mistaken

localStorage["form1"] = $("#form1").serialize();

saves all information from form 1 on the format key1=value1&key2=value2


localStorage["form2"] = $("#form2").serialize();

saves all information from form 2
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mokaid83
ID: 39864464
my friend . you are telling me about form1 and form2 while i have only one form which is "mainform" ...
each user could save the same form more than one time in offline mode

would you please show me the code for this purpose
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39864626
Sorry, you were not being clear.

You said I might have more than one form to save -I of course assume you meant you as the creator of the website.

You need to elaborate on how a user may have more than one form to save.

If it is the same form, each save should overwrite the previous form, no?
0
 

Author Comment

by:mokaid83
ID: 39864651
yes exactly .. that is what is happening right now
and i want to avoid the overwriting
i guess you got me now
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39864674
Not quite. What event makes it important to store a new form?
That they leave and come back?, then you can do

var now = new Date().getTime()

localStorage["form"+now] = $("#form1").serialize();

var list = localStorage["listOfSaves"];
if (list) localStorage["listOfSaves"]=list.split("|").push(now).join("|");
else localStorage["listOfSaves"]=[now];

Open in new window


now you can use the listOfSaves to get the saved forms
0
 

Author Comment

by:mokaid83
ID: 39864740
thats what i have been to explain all the day ..
you got me now ..

Thanks a lot friend
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question