Results 1 to 8 of 8

Thread: Saving Data with Java?

  1. #1
    Join Date
    Apr 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Saving Data with Java?

    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,

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    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.

  3. #3
    Join Date
    Apr 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by keyboard1333 View Post
    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

    HTML Code:
    <!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>
    Last edited by ceerup; 05-01-2012 at 10:33 PM.

  4. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Please use the forum's bbcode tags to make it more readable:

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


    HTML Code:
    <!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
    Tested in IE9 - Make sure you have cookies enabled...


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

  5. #5
    Join Date
    Apr 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by ceerup; 05-01-2012 at 07:08 PM.

  6. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    In this line here -
    Code:
    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)

  7. #7
    Join Date
    Apr 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by keyboard1333 View Post
    In this line here -
    Code:
    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?
    Last edited by ceerup; 05-01-2012 at 10:25 PM. Reason: Mistake

  8. #8
    Join Date
    Apr 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Okay so it seems that i have to make it save to the SD card i read on another forum, ill look into it
    Last edited by ceerup; 05-02-2012 at 05:12 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •