[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

localstorage html forms

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
mokaid83
Asked:
mokaid83
  • 4
  • 4
1 Solution
 
Michel PlungjanIT ExpertCommented:
How about


localStorage["form1"] = $("#form1").serialize();
localStorage["form2"] = $("#form2").serialize();
0
 
mokaid83Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mokaid83Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
mokaid83Author Commented:
yes exactly .. that is what is happening right now
and i want to avoid the overwriting
i guess you got me now
0
 
Michel PlungjanIT ExpertCommented:
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
 
mokaid83Author Commented:
thats what i have been to explain all the day ..
you got me now ..

Thanks a lot friend
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now