1

I've been using this tutorial online to try and save the input value for one of my form fields.

So far without any success.

Am I doing something wrong?

<script type="text/javascript">
    var today = new Date();
    var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

    function setCookie(name, value) {
        document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
    }

    function storeValues(form)  {
        setCookie("email", form.email-field.value);
        return true;
    }

    if(email = getCookie("email")) document.getElementById('login-form').email-field.value = email-field;

     function getCookie(name) {
        var re = new RegExp(name + "=([^;]+)");
        var value = re.exec(document.cookie);
        return (value != null) ? unescape(value[1]) : null;
     }

     document.write(getCookie("email"));
</script>

HTML:

<form action="" method="post" id="login-form">
<input class="field" type="text" name="email-field" placeholder="e-mail" style="text-transform: lowercase;" autofocus>
<br>
<input class="field" type="password" name="pass" placeholder="password">
<button type="submit"></button>
Nick
  • 1,417
  • 1
  • 14
  • 21
Nikk
  • 7,384
  • 8
  • 44
  • 90
  • your if clause if(email = getCookie("email")) probably needs to be: if(email == getCookie("email")), and you might wanna consider using id attributes for input fields int he form, for example – jyrkim Nov 29 '14 at 11:46
  • Will this make it work? – Nikk Nov 29 '14 at 12:15
  • 1
    Not quite. Anyway, I just made some changes that made it work :-) – jyrkim Nov 29 '14 at 13:21

1 Answers1

1

Your setCookie method's document.cookie part was okay, so I only had to make a couple of changes to make it work. For test purposes, I also changed the form a bit. Here is the code:

<form action="Cookies.html" method="post" id="login-form">
    <input class="field" type="text"  onkeydown="if (event.keyCode == 13) document.getElementById('submitButton').click()" id="emailField" name="email-field" placeholder="e-mail" style="text-transform: lowercase;" autofocus>
    <br>
    <input class="field" type="password" id="password" name="pass" placeholder="password">
    <br>
    <br> 
    <button id="submitButton" onclick="setCookie('email', 'emailField')" type="submit">set Cookie email</button>
    <br>
    <button onclick="setCookie('password', 'password')" type="button">set Cookie password</button>
    <br>
    <button onclick="displayCookieValue('email')" type="button">display Cookie email</button>
    <br>
    <button onclick="displayCookieValue('password')" type="button">display Cookie password</button>  
    <br>
   </form>

<div id="value"></div>

<script>

        var today = new Date();
        var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

        function setCookie(name, id) {
            var element = document.getElementById(id);
            var elementValue = escape(element.value);

            document.cookie = name + "=" + elementValue + "; path=/; expires=" + expiry.toGMTString();
            console.log(document.cookie);
        }

        function storeValues(form) {
            setCookie("email", form.email - field.value);
            return true;
        }

        function displayCookieValue(name) {
            var value = getCookie(name);
            var element = document.getElementById("value");
            element.innerHTML = "Cookie name: "+ name + ", value " + value;

        }

        function getCookie(name) {
            var re = new RegExp(name + "=([^;]+)");
            var value = re.exec(document.cookie);
            return (value != null) ? unescape(value[1]) : null;
        }
</script>

Note, it also stores the password value as the cookie value which in production is probably not a good idea :-) Also I tested it locally by using Apache server on my computer.

Screenshot below displays Chromes resources:

enter image description here

jyrkim
  • 2,849
  • 1
  • 24
  • 33
  • I only need the email stored. So I guess I'd only use the first one... However, you are using 'onclick' for the button. Um, if I change the button type to 'submit' because I need to submit the form. Will the cookie be saved? – Nikk Nov 29 '14 at 19:03
  • @Boris, yes it saves. I just checked it out, it appears on Chrome's resources as a cookie for email when saved with a submit button like this – jyrkim Nov 29 '14 at 19:22
  • @Boris one more thing I forgot to mention, when using submit button, you need to set a value for action attribute in the form tag:
    – jyrkim Nov 29 '14 at 19:27
  • Seriously you went out of your way to help me with this. Thank you. One more question, is it possible to extend the expiry time to say 20 years? I know its not possible to set it to infinite. – Nikk Nov 29 '14 at 22:03
  • One more question, since I won't be calling the values via a button, do I still need this `function displayCookieValue(name) { var value = getCookie(name); var element = document.getElementById("value"); element.innerHTML = "Cookie name: "+ name + ", value " + value; }` ? – Nikk Nov 29 '14 at 22:07
  • Is is possible to attach the `setCookie('email', 'emailField')` to the form rather than the button. Because, what if they press enter without clicking the button? – Nikk Nov 29 '14 at 22:15
  • @Boris about the expiry date, yes it's possible to set the expiry date 20 years from now. Second question regarding: displayCookieValue(name) method. That method was only used for testing/demonstration purposes, because cookies aren't visible to the end user like most form elements are. So you don't need that method :-) – jyrkim Nov 30 '14 at 11:31
  • Yes it's possible start form submission and cookie save process with an enter key press. You might find the following stack question useful: http://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box So if you add the following attribute into your within the email input tag: onkeydown="if (event.keyCode == 13) document.getElementById('submitButton').click()" and add id attribute: id="submitButton" to submit button, that will do the trick. I updated the form. – jyrkim Nov 30 '14 at 11:50
  • @Boris About forms and submission: "The Submit Button, defines a button for submitting a form to a form-handler. The form-handler is typically a server page with a script for processing input data. The form-handler is specified in the form's action attribute" Source: http://www.w3schools.com/html/html_forms.asp Overall, this was very good question about Cookies and I found it interesting because I hadn't used JavaScript with Cookies before. I had used Cookies with Ruby on Rails, so it was nice to work with Cookies with JavaScript too :-) – jyrkim Nov 30 '14 at 11:56