Solved

localstorage html forms

Posted on 2014-02-16
9
357 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now