Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  94] [ 1]  / answers: 1 / hits: 42402  / 9 Years ago, sun, may 24, 2015, 12:00:00

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>

More From » forms

 Answers
27

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.


[#66493] Wednesday, May 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiyam

Total Points: 448
Total Questions: 96
Total Answers: 92

Location: Philippines
Member since Sat, Jul 11, 2020
4 Years ago
;