Solved

localstorage html forms

Posted on 2014-02-16
9
367 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

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!

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

717 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