/    Sign up×
Community /Pin to ProfileBookmark

Use cookies for forms

Well,this script use cookies to store information about user click.All ideas and codes can help me.Thanks to all.
HTML+JavaScript:

[code=html]
<body onload=”delete_cookie (‘selected’)”></body>
<form><select>
<option value=”Empty” onclick=”ck (’empty’)”> </option>
<option value=”1″ onclick=”ck (‘1’)”>1</option>
<option value=”2″ onclick=”ck (‘2’)”>2</option>
</select>
</form>
<script>
function ck(name){
document.cookie=”selected”+”=”+escape (name);}
function get(){
var get_cookie=document.cookie.indexOf (“selected”);
if (get_cookie==-1){
alert (‘No think is selected or your cookies are disabled.’);}
else{
alert (document.cookie);}
}
function delete_cookie(name)
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() – 1 );
document.cookie = name += “=; expires=” + cookie_date.toGMTString();}
</script>
<input type=”button” value=”View the selected” onclick=”get()”>
<input type=”button” value=”Delete information” onclick=”delete_cookie (‘selected’)”>
[/code]

I write and use the script,all thinks work perfectly.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsSep 29.2008 — most browsers do not recognise an option on click event

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
function zxcCreateCookie(zxcnme,zxcv,zxcdays){
document.cookie=zxcnme+'='+zxcv+';expires='+new Date(new Date().getTime()+zxcdays*86400000).toGMTString();+';path=/';
}

function zxcReadCookie(zxcnme){
zxcnme+='=';
var zxcsplit = document.cookie.split(';');
for(var zxc0=0;zxc0<zxcsplit.length;zxc0++) {
var zxcs=zxcsplit[zxc0];
while (zxcs.charAt(0)==' ') zxcs=zxcs.substring(1,zxcs.length);
if (zxcs.indexOf(zxcnme)==0) return zxcs.substring(zxcnme.length,zxcs.length);
}
return null;
}

function Cookie(obj){
zxcCreateCookie(obj.name,obj.selectedIndex,1);
}

function Restore(nme){
var val=zxcReadCookie(nme);
if (val){
document.getElementsByName(nme)[0].selectedIndex=val;
}
}
/*]]>*/
</script></head>

<body onload="Restore('tst');">
<form><select name="tst" onchange="Cookie(this);">
<option value="Empty">0 </option>
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<input type="button" name="" value="Delete" onclick="zxcCreateCookie('tst','',-1)"/></form>
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@spiderman11authorSep 29.2008 — Help me if you can.
Copy linkTweet thisAlerts:
@spiderman11authorSep 29.2008 — Here is new script.I try with IE but don't work.
<i>
</i>&lt;body onload="delete_cookie ('selected')"&gt;&lt;/body&gt;
&lt;form name="Was"&gt;&lt;select&gt;
&lt;option value="Empty" name="emp" onclick="ck ('empty')"&gt; &lt;/option&gt;
&lt;option value="1" name="x1" onclick="ck ('1')"&gt;1&lt;/option&gt;
&lt;option value="2" name="x2" onclick="ck ('2')"&gt;2&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;
&lt;script&gt;
function ck(name){
document.cookie="selected"+"="+escape (name);}
function get(){
var get_cookie=document.cookie.indexOf ("selected");
if (get_cookie==-1){
alert ('No think is selected or your cookies are disabled.');}
else{
alert (document.cookie);}
}
function delete_cookie(name)
{
var cookie_date = new Date ( ); // current date &amp; time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = name += "=; expires=" + cookie_date.toGMTString();}
&lt;/script&gt;
&lt;input type="button" value="View the selected" onclick="get()"&gt;
&lt;input type="button" value="Delete information" onclick="delete_cookie ('selected')"&gt;


&lt;script&gt;
var h=document.Was.x1;
var h2=document.Was.x2;
var h3=document.Was.emp;
if (h.selected){
ck ('1');}
if (h2.selected){
ck ('2');}
if (h3.selected){
ck ('empty');}
&lt;/script&gt;

×

Success!

Help @spiderman11 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...