PDA

View Full Version : Saving Data with Java?



ceerup
04-30-2012, 01:55 AM
I have a java script calculator that i would like the numbers people enter and the information appear next time they use the calculator,

For example they enter 2+2 and the calculator says 4, when they exit the calculator and come back, the answer "4" is still in the box

anyone know how to go about and do this? i found a website
http://www.w3schools.com/js/js_cookies.asp

but im confused how to write this into my code,

keyboard
04-30-2012, 02:20 AM
Please post a link to the page on your site that contains the problematic script or the source code, so we can check it out.

ceerup
04-30-2012, 02:28 AM
Please post a link to the page on your site that contains the problematic script or the source code, so we can check it out.

here you go, im working on a project in eclipse for an application, and i want the user to come back to the same data they had before they exited the application



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calc</title>

<script type="text/javascript">

function calculate() {
var one = document.getElementById("one").value*1;
var two = document.getElementById("two").value*1;
var three = document.getElementById("three").value*1;
document.getElementById("answer").value=((one-two)/three).toFixed(2);
;
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}

</script>
</head>
<body>
Enter Friend Score
<br>
<br><input type="text" id="one" /><hr>Enter Your Score<br><br><input type="text" id="two" /><br><br><hr>Enter Your Average Score<br><br><input type="text" id="three" /><br><br><button onclick="calculate();">Calculate</button> <br id="result"></center></br> Total <input type="text" readonly="readonly" id="answer" /><br><br></center>

</body>
</html>

</td>
</tr>
</table>
</center>
</body>
</html>

keyboard
04-30-2012, 06:43 AM
Please use the forum's bbcode tags to make it more readable:

for php code............
<?php /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calc</title>
<script type="text/javascript">
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
document.getElementById("answer").value = unescape(y);
}
}
}
function calculate() {
var one = document.getElementById("one").value*1;
var two = document.getElementById("two").value*1;
var three = document.getElementById("three").value*1;
document.getElementById("answer").value=((one-two)/three).toFixed(2);
setCookie('remCalc',((one-two)/three).toFixed(2),'100');
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
</script>
</head>
<body onload="getCookie('remCalc')">
Enter Friend Score
<br>
<br><input type="text" id="one" /><hr>Enter Your Score<br><br><input type="text" id="two" /><br><br><hr>Enter Your Average Score<br><br><input type="text" id="three" /><br><br><button onclick="calculate();">Calculate</button> <br id="result"></center></br> Total <input type="text" readonly="readonly" id="answer" /><br><br></center>

</body>
</html>

From here (http://www.w3schools.com/js/js_cookies.asp)
Tested in IE9 - Make sure you have cookies enabled...


Also, please don't post multiple copies of your question cerrup.

ceerup
05-01-2012, 06:55 PM
sorry about that, im a newbie when it comes to any of this stuff

how do i set the expiration date on it? because when i close the calculator and come back its not there, its only there when im switching through menus on my emulator

keyboard
05-01-2012, 10:14 PM
In this line here -

setCookie('remCalc',((one-two)/three).toFixed(2),'100');

100 days is the expiry date, so that shouldn't be a problem...
What browser are you using? Do you have cookies enabled? Are you deleting them when you close the browser? (Its one of the settings)

ceerup
05-01-2012, 10:19 PM
In this line here -

setCookie('remCalc',((one-two)/three).toFixed(2),'100');

100 days is the expiry date, so that shouldn't be a problem...
What browser are you using? Do you have cookies enabled? Are you deleting them when you close the browser? (Its one of the settings)

Hello keyboard, im using an html document on eclipse, then running it on the emulator, also when i do it on firefox it doesnt work either, it only works when my java application is running on eclipse, other than that when i exit the app and come back its all gone

yes cookies are enabled, im not deleting them when i close the browser

EDIT: It works on the browsers, just not in the android emulator, maybe thats only because its an emulator right?

ceerup
05-01-2012, 10:31 PM
Okay so it seems that i have to make it save to the SD card i read on another forum, ill look into it