Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    How do i make my website remember user input.

    Ok, so if you navigate ovr to my website "http://www.mancunianmacca.co.cc" you will notice that a box pops up asking for your name. When you type it in it adds it to the page. Is there a way I can get this to remember the users input and only ask them once then remember. Like cookie or anything. If you need any extra things. Just ask.
    Last edited by MancunianMacca; 03-03-2012 at 01:04 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Yes, you need to use a cooke (althugh users may disable or erase them). Cookies have been dealt with very many times in this forum - have you tried using the search feature?

    Here is what you want:-

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function changeText2(){
    var userInput = document.getElementById('uInput').value;
    userInput = userInput.replace(/[^a-z\-\s\']/gi,"");  // letters space hyphen apostrophe only
    userInput = userInput.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    userInput = userInput + "!";  // add a ! 
    document.getElementById('boldStuff2').innerHTML = userInput;
    document.getElementById("stuff").style.display="none";
    createCookie ("UserName",userInput,365);
    }
    
    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else {var expires = ""}
    document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }
    
    function eraseCookie(name) {
    createCookie(name,"",-1);
    }
    </script>
    </head>
    
    <body>
    <p>Welcome to the site <b id='boldStuff2'></b> </p> 
    <div id = "stuff" style="display:block">
    Enter your name here <input type='text' id='uInput' onchange = "changeText2()">
    </div>
    
    <script type="text/javascript">
    // this script must come after the divs
    if(readCookie('UserName')) {
    var n = readCookie('UserName');
    document.getElementById("boldStuff2").innerHTML =  n;
    document.getElementById("stuff").style.display="none";
    }
    
    </script>
    
    </body>
    </html>
    Quizmaster: What is the easternmost destination on the Trans-Siberian Railway?
    Contestant: Vienna.
    Last edited by Philip M; 03-02-2012 at 07:10 PM. Reason: Improved

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    MancunianMacca (03-03-2012)

  • #3
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Nope but I will now, thanks

  • #4
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    Yes, you need to use a cooke (althugh users may disable or erase them). Cookies have been dealt with very many times in this forum - have you tried using the search feature?

    Quizmaster: What is the easternmost destination on the Trans-Siberian Railway?
    Contestant: Vienna.
    Just done a search and I can't find anything that relates to what I need. I haven't used cookies before. I was trying to figure this out myself online and founf somthing like "session storage" would that be a better way than cookies. Anywho please help!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.
    Ok so i've got this done and evrything works great exept from the write in bit. Here is the code i have in an extrnal .js file

    Code:
    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)
        {
        return unescape(y);
        }
      }
    }
    
    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;
    }
    and to make it write in i'm trying to use this code.

    Code:
    <script>
    function checkCookie()
    {
    var username=getCookie("username");
    var addtext = username;
    if (username!=null && username!="")
      {
      document.getElementById("addtext").innerHTML = addtext;
      }
    else
      {
      username=prompt("Please enter your name:","");
      if (username!=null && username!="")
        {
        setCookie("username",username,365);
        }
      }
    }
    </script>
    Now the prompt works so the .js file is working, but it will not write, I had it wirting when i used "writeln" but it wrote to a blank page not to the page.

    Please help, Thanks.

  • #7
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.
    I used your code and it works great... But, I want it to be a Prompt not a text field. I tried to adapt on your code to try this but no luck. Please help...
    Code I tried...
    Code:
    <body>
    <p>Welcome to the site <b id='boldStuff2'></b> </p> 
    <div id = "stuff" style="display:block">
    <script type="text/javascript">
    changeText2=prompt("Please enter your name:","");
    </script>
    
    </div>

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Code:
    <script>
    function checkCookie()
    {
        var username=getCookie("username");
        if (username ==null || username =="")
            username=prompt("Please enter your name:","");
            setCookie("username",username,365);
        }
        document.getElementById("addtext").innerHTML = username
      }
    }
    </script>
    LOGIC. You need to learn to use simple logic. Nothing is going to *AUTOMATICALLY* put the prompted-for name into the innerHTML. So you need to do that NO MATTER if the username comes from a cookie or from the prompt.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    MancunianMacca (03-03-2012)

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Prompt is a very crude and amateurish method of obtaining user data. But surely you can make that simple alteration yourself? I get the impression that you are trying to run before you can walk - or even crawl. changeText2 is the name of a function, not a variable. Do you understand the difference?

    Old Pedant:-

    A realist drinks it no matter how much there is.
    Does that include the poison in the jewelled glass you used to have in your sig?

    Personally I believe the glass is superfluous - you get the same result quicker and cheaper by drinking straight from the bottle.

    Lady Astor said to Winston Churchill "If I was married to you, I would put poison in your whisky".
    To which Churchill replied "If I was married to you, I would drink it".
    Last edited by Philip M; 03-03-2012 at 10:45 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Thumbs up

    Quote Originally Posted by Philip M View Post
    Prompt is a very crude and amateurish method of obtaining user data. But surely you can make that simple alteration yourself? I get the impression that you are trying to run before you can walk - or even crawl. changeText2 is the name of a function, not a variable. Do you understand the difference?

    Old Pedant:-

    A realist drinks it no matter how much there is.
    Does that include the poison in the jewelled glass you used to have in your sig?

    Personally I believe the glass is superfluous - you get the same result quicker and cheaper by drinking straight from the bottle.

    Lady Astor said to Winston Churchill "If I was married to you, I would put poison in your whisky".
    To which Churchill replied "If I was married to you, I would drink it".
    I think you're right, but i'm finding it so hard to learn It's just like i'm not learning at all just copying others code. I am really trying to learn and I know that I am proberbly really annoying. So I couldn't get the prompt to work so I used your original text box code for now. I'm going to keep trying but at the lower level stuff.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by MancunianMacca View Post
    I think you're right, but i'm finding it so hard to learn It's just like i'm not learning at all just copying others code. I am really trying to learn and I know that I am proberbly really annoying. So I couldn't get the prompt to work so I used your original text box code for now. I'm going to keep trying but at the lower level stuff.
    That is precisely the impression that I get. You must master the basics first. It is like learning a spoken langauge - the first steps are difficult, but it does get easier. But if you do not master the fundamentals you will never get anywhere.

    It was as easy as this:-

    var userInput = prompt("Please enter your name", "");

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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