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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookies: Displaying The Input

    Hi, I have just started JavaScript a few weeks back, and I'm having this problem with the cookies. I'am trying to display whatever is inputted into the form in a alert box but it just appears blank. Could someone please explain what I'am doing wrong thanks.

    Code:
    <body>
    <script language="JavaScript" type="text/JavaScript">
    
    
    function writeCookie(name, value, hours)
    {
      var expire = "";
      if(hours != null)
      {
        expire = new Date((new Date()).getTime() + hours * 3600000);
        expire = "; expires=" + expire.toGMTString();
      }
      document.cookie = name + "=" + escape(value) + expire;
    }
    
    function readCookie(name)
    {
      var cookieValue = "";
      var search = name + "=";
      if(document.cookie.length > 0)
      { 
        offset = document.cookie.indexOf(search);
        if (offset != -1)
        { 
          offset += search.length;
          end = document.cookie.indexOf(";", offset);
          if (end == -1) end = document.cookie.length;
          cookieValue = unescape(document.cookie.substring(offset, end))
    	  
        }
      }
      return cookieValue;
    }
    
    
    
    
    </script>
    
    
    
    
    <p>Writing a cookie</p>
    <form name="form1" method="post" action="">
      <p>name 
        <input name="Name" type="text" id="Name">
      </p>
      <p>
        <input type="button" name="Submit" value="WriteCookie" onclick="alert(document.cookie);">
    	
      </p>
    </form>
    
    
    
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Probably because you have nothing in the cookie to display.
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by chrishirst View Post
    Probably because you have nothing in the cookie to display.
    Usual unhelpful kind of response from oh-so-clever chrishurst.

    You are not calling the writeCookie() function so when you make the alert no cookie exists yet.

    onclick="writeCookie ('CookieName', 'CookieValue', 365)"
    where cookiename and cookie value are literals. Here the value is document.getElementbyId("Name").value;
    365 is the number of days persistence you desire.

    Put your alert at the end of the writeCookie() function.

    “The essence of being human is that one does not seek perfection.” George Orwell, English novelist and journalist, 1903-1950.
    Last edited by Philip M; 06-08-2014 at 01:00 PM.

    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.

  • #4
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    I don't believe in spoon-feeding instant answers to ANYONE.


    People learn more by having to WORK to get an answer. Giving them instant solutions only teaches that some idiot will do the work for them if they ask in the right place, you providing what the problem is and how to solve it, has NOT taught the TS one thing about fault finding in code, only that somebody else can.

    The real world is not junior (grade) school where the class teacher will tell you what the answer is and you have to remember it. Would YOU employ someone that had to ask a forum for how to debug code?
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by chrishirst View Post
    I don't believe in spoon-feeding instant answers to ANYONE.


    People learn more by having to WORK to get an answer. Giving them instant solutions only teaches that some idiot will do the work for them if they ask in the right place, you providing what the problem is and how to solve it, has NOT taught the TS one thing about fault finding in code, only that somebody else can.

    The real world is not junior (grade) school where the class teacher will tell you what the answer is and you have to remember it. Would YOU employ someone that had to ask a forum for how to debug code?
    That is not in accordance with what has for long seen as the appropriate attitudes to be exhibited in this forum. The unspoken rule has long been "if you are unwilling or unable to help, then keep quiet". Newcomers need more explicit assistance than more experienced people.
    You give the impression that your purpose in posting is to shout "Look everyone how amazingly clever I am". And your language ("idiots") and phraseolgy seem to be little more than a mask for your ill-concealed contempt for people who are not half as brilliant as you (think you) are. I suspect that to you everyone else is an idiot. RTFM or equivalent is not a response which will win you many friends here.

    And, yes, I would employ someone who had the wit and humility to acknowledge that they did not know something, and asked for assistance in an appropriate place. I certainly don't know everything - do you?

    "What wisdom can you find that is greater than kindness?" - Jean Jacques Rousseau

    “There is overwhelming evidence that the higher the level of self-esteem, the more likely one will be to treat others with respect, kindness, and generosity.”- Nathaniel Branden
    Last edited by Philip M; 06-08-2014 at 02:24 PM. Reason: typo

    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
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Newcomers need more explicit assistance than more experienced people
    And HOW are "newcomers" going to actually LEARN if they get provided a ready made solution, and no I do not "know everything", however I do know how to be able to solve any code problem (C, C++, Python, ASP, PHP, js etc.) MYSELF without having to ask, and I can debug my code and have been able to do in 20+ years of programming, simply because I was taught how to learn NOT how to "phone a friend"

    But any how, thanks! Because you have reminded me why I could not be arsed posting here for almost nine years.


    Goodbye!
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by chrishirst View Post
    But any how, thanks! Because you have reminded me why I could not be arsed posting here for almost nine years.


    Goodbye!
    For this relief much thanks. - Hamlet

    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
    •