最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting cookies with form input - Stack Overflow

programmeradmin6浏览0评论

I am trying to make a username cookie using the user's input to a web form. However it's not working and I don't know why. Do you know what the problem is?

<form>
    <input type="text" value="Enter Your Nickname" id="nameBox">
    <input type="button" value="Go!" id="submit" onClick="setCookie();">
<form>


<script>
    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();
    }
    //this should set the UserName cookie to the proper value;
    function storeValues(form){
        setCookie("userName", form.submit.value);
        return true;
    }

</script>
</body>

I am trying to make a username cookie using the user's input to a web form. However it's not working and I don't know why. Do you know what the problem is?

<form>
    <input type="text" value="Enter Your Nickname" id="nameBox">
    <input type="button" value="Go!" id="submit" onClick="setCookie();">
<form>


<script>
    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();
    }
    //this should set the UserName cookie to the proper value;
    function storeValues(form){
        setCookie("userName", form.submit.value);
        return true;
    }

</script>
</body>
Share Improve this question edited Dec 16, 2021 at 6:32 Shahid momin 33 bronze badges asked May 24, 2015 at 3:56 Hans.GundlachHans.Gundlach 4311 gold badge6 silver badges11 bronze badges 7
  • There's no putCookie function in the javascript code; looks like you called it setCookie – frenchie Commented May 24, 2015 at 4:04
  • fixed that it still doesn't work – Hans.Gundlach Commented May 24, 2015 at 4:11
  • 1 is the escape(value) function defined? what is it returning – lyjackal Commented May 24, 2015 at 4:17
  • Why is your script outside of body and head inside html? That’s invalid markup. Also, did you define form and submit? – Sebastian Simon Commented May 24, 2015 at 4:19
  • I've put the script in the body.Maybe I'm not checking for cookies right. – Hans.Gundlach Commented May 24, 2015 at 4:25
 |  Show 2 more ments

2 Answers 2

Reset to default 8

You can check below code, it might help you.

<html>
<head>
    <script>
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 putCookie(form)
                //this should set the UserName cookie to the proper value;
  {
   setCookie("userName", form[0].usrname.value);

    return true;
  }

  </script>
</head>
<body>
<form>
 <input type="text" value="Enter Your Nickname" id="nameBox" name='usrname'>
 <input type="button" value="Go!" id="submit" onclick="putCookie(document.getElementsByTagName('form'));">
</form>
</body>


</html>

While defined function name should be putCookies instead of storeValues and function call you can do this way: putCookie(document.getElementsByTagName('form'));

Inside the function definition cookie values can be get from the form as below: setCookie("userName", form[0].usrname.value);

Form element should have attribute : name='usrname'

It will surely set cookies for your username with form element.

Change the form to:

<form onsubmit="storeValues(this)">
<input type="text" value="Enter Your Nickname" id="nameBox">
<input type="submit" value="Go!" id="submit">
<form>

And storeValues(form) function to:

{
   setCookie("userName", form.nameBox.value);
   return true;
}
发布评论

评论列表(0)

  1. 暂无评论