help me with Document.form cookies

L

Leroy Frye

hai all.......

i`m new in javascript...
i got crazy with this script because i was search in many tutorial and
learn online and do "try and error"
for 2 weeks to get variable value but still undefined :(

how i can get cookies all value without changing this script with
document.cookie?


<script language="javascript" type="text/javascript"><!--

function checkMemberForm24(frm){
if(!validateName(frm.elements['form[first_name]'].value)){
alert('Please enter valid First Name!');
frm.elements['form[first_name]'].focus();
return false;
}
if(!validateName(frm.elements['form[last_name]'].value)){
alert('Please enter valid Last Name!');
frm.elements['form[last_name]'].focus();
return false;
}
if((frm.elements['form[sex]'].value == 'Yes') && (frm.elements['form
[age]'].value == '')){
alert('Age and sex Required!');
frm.elements['form[age]'].focus();
return false;
}

--></script>


<div id="divForm24" class="formContent" id="frmMember24"
name="frmMember24">
<form action="" method="POST" onsubmit="return checkMemberForm24
(this)">
<div style="padding:5px;font-weight:bold;">Member Information</div>
<div class="formLine">
<div class="formSpace"><u>Please enter your Information</u></div>
</div>
<div style="clear:both;height:0px;">
<input type="hidden" name="ow" value="ProcessMember"/>
</div>
<div class="formLine">
<div class="formCaption">First Name :</div>
<div class="formControl">
<input type="text" class="formControlText" name="form[first_name]"
value="John"/>
</div>
</div>
<div class="formLine">
<div class="formCaption">Last Name :</div>
<div class="formControl">
<input type="text" class="formControlText" name="form[last_name]"
value="Doe"/>
</div>
</div>
<div class="formLine">
<div class="formCaption">Sex :</div>
<div class="formControl">
<select name="form[sex]" value=""/>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="formLine">
<div class="formCaption">Age :</div>
<div class="formControl">
<input type="text" class="formControlText" style="width:50px;"
maxlength="4" size="2" name="form[age]" value="35"/>
</div>
</div>
<br/>

<div class="formLine">
<div class="formSpace" style="text-align:center;width:100%;">
<input type="image" src="images/buttons/submit.gif" alt="Submit >>"
id="submit"/>
<br/>
</div>
</div>
</form>
</div>
</div>


i was try this

document.cookie='firstname='+document.getElementById('form
[first_name]').value;
document.cookie='firstname='+document.forms['form[first_name]'].value;
document.cookie='firstname='+document.forms.frm.elements['form
[first_name]'].value;
document.cookie='firstname='+document.forms.[frmMember24].elements
['form[first_name]'].value;
document.cookie='firstname='+document.forms.[formControlText].elements
['form[first_name]'].value;
and etc


but all i type still undefined.
can someone explaint it to me how to get it?
thanks
 
D

DonO

i was try this
document.cookie='firstname='+document.getElementById('form
[first_name]').value;
document.cookie='firstname='+document.forms['form[first_name]'].value;
document.cookie='firstname='+document.forms.frm.elements['form
[first_name]'].value;
document.cookie='firstname='+document.forms.[frmMember24].elements
['form[first_name]'].value;
document.cookie='firstname='+document.forms.[formControlText].elements
['form[first_name]'].value;
and etc

but all i type still undefined.
can someone explaint it to me how to get it?
thanks

I'm not sure I'm following what you're trying to do, but if you're
trying to check for a cookie on the same page that sets the cookie, it
won't be defined I believe. You'd have to set the cookie, then go to
another page that checks to see if it's been set.

D.
 
V

VK

how i can get cookies all value without changing this script with
document.cookie?

Without commenting or analyzing cookie-unrelated parts of your script,
set/get cookie is very simple if allowed by browser settings:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Test</title>
<script type="text/javascript">
function setCookie() {
document.cookie =
document.forms[0].elements['FirstName'].value;
}

function getCookie() {
window.alert(document.cookie);
}
</script>
</head>
<body>
<div>
<form action="" method="POST">
<input type="hidden" name="FirstName" value="John">
<button type="button" onclick="setCookie()">Set Cookie</button>
<button type="button" onclick="getCookie()">Get Cookie</button>
</form>
</div>
</body>
</html>

Please not that this way you are creating session-long cookies - thus
they exist only until the last instance of your browser is closed. To
make them stay longer, you have to add "expires" value. Say to keep
cookie for 30 days from now:
....
var now = new Date();
document.cookie =
document.forms[0].elements['FirstName'].value +
';'+ (now.setDate(now.getDate()+30)).toGMTString();
....
 
V

VK

Oops, missed "; expires=" part of the string:
...
 var now = new Date();
document.cookie =
document.forms[0].elements['FirstName'].value +
'; expires='+ (now.setDate(now.getDate()+30)).toGMTString();
...
 
L

Leroy Frye

Oops, missed "; expires=" part of the string:
 ...
 var now = new Date();
 document.cookie =
  document.forms[0].elements['FirstName'].value +
  '; expires='+ (now.setDate(now.getDate()+30)).toGMTString();
  ...

would you mine to explaint
what mean of " document.forms " ?
what mean of " [0] " ?
what mean of " ; " before "expires="?
what mean of " elements before firstname " ?

on local computer your code run and retrive cookies
document.cookie = document.forms[0].elements['FirstName'].value + ';
expires='+ (now.setDate(now.getDate()+30)).toGMTString();

then when i upload into hosting, still undefined

cause your idea, i make this code and was able to read cookies from
local in my computer too

here the code that i make :
document.cookie = "firstname=" + frm.elements['form
[first_name]'].value;

but when i try into hosting, still undefined.

i dont know what is going on with this :(
 
V

VK

would you mine to explaint
what mean of " document.forms " ?

Collection of all forms on the page from top to bottom.
what mean of " [0] " ?

document.forms[0] refers to the first form from the bottom of the page
(index 0)
what mean of " ; " before "expires="?

Cookie is a string in form "field1=value1; field2=value2" so ; is the
separator between key/value pairs. Read more at http://www.quirksmode.org/js/cookies.html
what mean of " elements before firstname " ?

document.forms[0].elements returns collection of all elements in the
first form on the page.

document.forms[0].elements['FirstName'] returns form element named
"FirstName" from the first form on the page.

Honestly I didn't use JavaScript to set cookies for ages, always
server-side and using higher level libraries. So it took some time to
recall all this low level cookie/Date stuff.

Updated working sample is below, online at http://www.whitenightsopera.com/tmp/_tmp3.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Test</title>
<script type="text/javascript">
function setCookie() {
var now = new Date();
now.setTime( now.getTime()+2592000000 );
document.cookie =
'FirstName='+document.forms[0].elements['FirstName'].value+
';expires='+ now.toUTCString();
}

function getCookie() {
window.alert(document.cookie);
}
</script>
</head>
<body>
<div>
<form action="" method="POST">
<input type="hidden" name="FirstName" value="John">
<button type="button" onclick="setCookie()">Set Cookie</button>
<button type="button" onclick="getCookie()">Get Cookie</button>
</form>
</div>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top