document cookies with frm.elements[form].value

L

Leroy Frye

hii all,
this script is nightmare for me, sometimes i got a value with cookies
but sometimes not, why?
sometimes i got like this :

new member was registered, here details :
first name : john
last name : doe
sex : m

but sometimes i got like this, here details :
new member was registered, here details :
first name : undefined
last name : undefined
sex : undefined

there is a something wrong? or browser support?

how i can get cookies in all browser?
how i can get cookies without an empty value come into my post??

here is my testing script:

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

function checkMemberForm24(frm){
document.cookie = "fname=" + frm.elements['form[first_name]'].value;
document.cookie = "lname=" + frm.elements['form[last_name]'].value;
document.cookie = "sex=" + frm.elements['form[sex]'].value;
alert(frm.elements['form[first_name]'].value);
alert(frm.elements['form[last_name]'].value);
alert(frm.elements['form[sex]'].value);
}
--></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="oa" 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>


thanks
 
S

Stevo

Leroy said:
hii all,
this script is nightmare for me, sometimes i got a value with cookies
but sometimes not, why?

It's because you're using session cookies. As soon as you close the
browser (or navigate to a different domain) it will discard session
cookies (and they aren't shared by different browsers).
<script language="javascript" type="text/javascript"><!--

function checkMemberForm24(frm){
document.cookie = "fname=" + frm.elements['form[first_name]'].value;
document.cookie = "lname=" + frm.elements['form[last_name]'].value;
document.cookie = "sex=" + frm.elements['form[sex]'].value;

You need to add the cookie expiry date that tells the browser that it
needs to be serialized to a file.

function setCookie(str,days_to_expiry)
{
var exp=new Date();
var one_day=1000*60*60*24;
exp.setTime(exp.getTime()+(one_day * days_to_expiry));
document.cookie=str+"; expires="+exp.toGMTString();
}

//store cookies from form and let them live for 5 days
setCookie(your_fname_cookie_string,5);
setCookie(your_lname_cookie_string,5);
setCookie(your_sex_cookie_string,5);
 
L

Leroy Frye

Leroy said:
hii all,
this script is nightmare for me, sometimes i got a value with cookies
but sometimes not, why?

It's because you're using session cookies. As soon as you close the
browser (or navigate to a different domain) it will discard session
cookies (and they aren't shared by different browsers).
<script language="javascript" type="text/javascript"><!--
function checkMemberForm24(frm){
document.cookie = "fname=" + frm.elements['form[first_name]'].value;
document.cookie = "lname=" + frm.elements['form[last_name]'].value;
document.cookie = "sex=" + frm.elements['form[sex]'].value;

You need to add the cookie expiry date that tells the browser that it
needs to be serialized to a file.

function setCookie(str,days_to_expiry)
{
   var exp=new Date();
   var one_day=1000*60*60*24;
   exp.setTime(exp.getTime()+(one_day * days_to_expiry));
   document.cookie=str+"; expires="+exp.toGMTString();

}

//store cookies from form and let them live for 5 days
setCookie(your_fname_cookie_string,5);
setCookie(your_lname_cookie_string,5);
setCookie(your_sex_cookie_string,5);


oh, are you sure that cookies not missing or undefined if i make
cookie exp date and serialized as a file?


here the code :

<script language="javascript" type="text/javascript"><!--
function checkMemberForm24(frm){
document.cookie = "fname=" + frm.elements['form[first_name]'].value;
document.cookie = "lname=" + frm.elements['form[last_name]'].value;
document.cookie = "sex=" + frm.elements['form[sex]'].value;
var exp=new Date();
var one_day=1000*60*60*24;
exp.setTime(exp.getTime()+(one_day * days_to_expiry));
document.cookie=str+"; expires="+exp.toGMTString();
setCookie(frm.elements['form[first_name]'].value,5);
setCookie(frm.elements['form[last_name]'].value,5);
setCookie(frm.elements['form[sex]'].value,5);
alert(frm.elements['form[first_name]'].value);
alert(frm.elements['form[last_name]'].value);
alert(frm.elements['form[sex]'].value);
}
--></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="oa" 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>
 
D

Dr J R Stockton

In comp.lang.javascript message <2178b8e5-e2f4-4eec-b6bc-050312420856@2g
2000prl.googlegroups.com>, Wed, 21 Oct 2009 06:30:14, Leroy Frye
var exp=new Date();
var one_day=1000*60*60*24;
exp.setTime(exp.getTime()+(one_day * days_to_expiry));
document.cookie=str+"; expires="+exp.toGMTString();

If I opened the page next Saturday afternoon, and you offered me a one-
day expiry, I would feel entitled to 'live' until the same time on
Sunday. The code fails to give that.

<FAQENTRY> Adding days - use of getDate setDate.
 
S

Stevo

Dr said:
In comp.lang.javascript message <2178b8e5-e2f4-4eec-b6bc-050312420856@2g
2000prl.googlegroups.com>, Wed, 21 Oct 2009 06:30:14, Leroy Frye


If I opened the page next Saturday afternoon, and you offered me a one-
day expiry, I would feel entitled to 'live' until the same time on
Sunday. The code fails to give that.

All it's promising is that your cookies will live that long. So feel
free to place them on the coffee table on a plate, and you'll be
guaranteed that they'll still be there tomorrow. Experiment needed.
Note, this doesn't apply in households with children, pets, or anyone
adult that really likes cookies.
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top