Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

localstorage html forms

Posted on 2014-02-16
9
Medium Priority
?
379 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
Independent Software Vendors: 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

636 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