/    Sign up×
Community /Pin to ProfileBookmark

Javascript Cookie Help Needed!!

Hi you helpful people,

I’ve managed to get over this coding issue before, but it’s giving me a headache and I think I’ve been making a simple task hard.

All I want to do is save the data that is put on a form to a cookie when a ‘save’ button is pressed and I want to clear the form and add 0’s (zero’s) in the spaces when a ‘clear’ button is pressed, my code so far is below, I’ve tried to add the cookie feature to it myself, but my last coding where I got it to work seems so complicated I can’t see how I managed to get it to work 😮

[CODE]<HTML><HEAD>
<SCRIPT language=”JavaScript”>
function Calculate()
{
footform.a3.value = parseFloat(footform.a2.value) – parseFloat(footform.a1.value);
footform.b3.value = parseFloat(footform.b2.value) – parseFloat(footform.b1.value);
footform.c3.value = parseFloat(footform.c2.value) – parseFloat(footform.c1.value);
footform.d3.value = parseFloat(footform.d2.value) – parseFloat(footform.d1.value);
footform.e3.value = parseFloat(footform.e2.value) – parseFloat(footform.e1.value);
footform.f3.value = parseFloat(footform.f2.value) – parseFloat(footform.f1.value);
footform.g3.value = parseFloat(footform.g2.value) – parseFloat(footform.g1.value);
footform.Subtotal.value = parseFloat(footform.a3.value) + parseFloat(footform.b3.value) + parseFloat(footform.c3.value) + parseFloat(footform.d3.value) + parseFloat(footform.e3.value) + parseFloat(footform.f3.value) + parseFloat(footform.g3.value);
footform.b1.value = footform.a2.value;
footform.c1.value = footform.b2.value;
footform.d1.value = footform.c2.value;
footform.e1.value = footform.d2.value;
footform.f1.value = footform.e2.value;
footform.g1.value = footform.f2.value;
}

</SCRIPT>

</HEAD>
<BODY>
<center>
<form NAME=”footform” action=”” method=POST>
<TABLE>
<TR align=”center”>
<TD width=”100″><B>DAY</B></TD>
<TD><B>Open Count</B></TD>
<TD><b>Close Count</b></TD>
<TD><b>Days Total</b></TD>
</TR>
<TR>
<TD><B>Monday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”a1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”a2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”a3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Tuesday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”b1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”b2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”b3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Wednessday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”c1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”c2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”c3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Thursday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”d1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”d2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”d3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Friday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”e1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”e2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”e3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Saturday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”f1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”f2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”f3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD><B>Sunday</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”g1″ SIZE=”12″ value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”g2″ SIZE=”12″ onkeyup=”Calculate();” value=”0″>
</TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”g3″ SIZE=”12″ value=”0″>
</TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD align=”center”><B>total</B></TD>
<TD>
<INPUT TYPE=”TEXT” NAME=”Subtotal” SIZE=”12″ value=”0″>
</TD>
</TR>
</TABLE><P>
<INPUT type=button value=Save>
&nbsp; <INPUT onclick=”” type=button value=”Clear Form”>
&nbsp; <INPUT type=submit value=send>
<P align=center>
<B>Todays Date:</B><BR>
<script language=”JavaScript”>
<!– Begin
days = new Array(7)
days[1] = “Sunday”;
days[2] = “Monday”;
days[3] = “Tuesday”;
days[4] = “Wednesday”;
days[5] = “Thursday”;
days[6] = “Friday”;
days[7] = “Saturday”;
months = new Array(12)
months[1] = “January”;
months[2] = “February”;
months[3] = “March”;
months[4] = “April”;
months[5] = “May”;
months[6] = “June”;
months[7] = “July”;
months[8] = “August”;
months[9] = “September”;
months[10] = “October”;
months[11] = “November”;
months[12] = “December”;
today = new Date(); day = days[today.getDay() + 1]
month = months[today.getMonth() + 1]
date = today.getDate()
year=today.getYear();
if (year < 2000)
year = year + 1900;
document.write(“<input name=’day’ size=25 type=’text’ value='”);
document.write (“”+ day + “, ” + month + ” ” + date + “, ” + year + “”)
document.write(“‘>”);
// End –>
</script>
</FORM>
</center>
</BODY>
</HTML>[/CODE]

Any help is greatfully received!!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JJul 07.2005 — The cookie information here may help you out

www.huntingground.freeserve.co.uk/scripts/cookies/the_cookie.htm
Copy linkTweet thisAlerts:
@benzen78authorJul 08.2005 — I had a look at that site, but that seems different to the way I have done things in the past, it seems a very long way around the problem, the script seems long to what I have done in the past....

the script I have used in the past looks a bit like this:

<SCRIPT language=JavaScript>

<!-- Hide from older browsers

var bites = document.cookie.split("; ");

function getCookie(name) {

for (i=0; i < bites.length; i++) {

nextbite = bites[i].split("=");

if (nextbite[0] == name)

return unescape(nextbite[1]);

}

return null; // if no match return null

}



var today = new Date();

var expiry = new Date(today.getTime() + 90 * 90 * 90 * 90 * 9000); // plus 28 days



function setCookie(name, value) { // use: setCookie("name", value);

if (value != null && value != "")

document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();

bites = document.cookie.split("; "); // update cookie bites

}



var userData = getCookie("userData") ||

"0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0!0";

var data = userData.split("!"); // convert cookie value to array



function setValues() {

with (document.weekend) {

m3.value = 0; m4.value = 0; m6.value = 0;

t3.value = 0; t4.value = 0; t6.value = 0;

w3.value = 0; w4.value = 0; w6.value = 0;

th3.value = 0; th4.value = 0; th6.value = 0;

f3.value = 0; f4.value = 0; f6.value = 0;

s3.value = 0; s4.value = 0; s6.value = 0;

su3.value = 0; su4.value = 0; su6.value = 0;

tt6.value = 0;

}

}



function alert2() {

if(confirm("This will reset the form - Are you sure?")) {

setValues();

}

else {

quit();

}

}



With buttons like:



<INPUT onclick="with (this.form) { data[0] = m1.value; data[1] = m2.value; setCookie('userData', data.join('!')); // cookie will store any length array history.go(0)" type=button value=Save>

<INPUT onclick="alert2('alert2();')" type=button value="Clear Form">

<INPUT type=submit value=send>



But I can't seem to get this to work again??
Copy linkTweet thisAlerts:
@emblemJul 08.2005 — here are some cookie functions s scavenged up

mabey they will help:
<i>
</i>function getCookieVal (offset) { <br/>
var endstr = document.cookie.indexOf (";", offset); <br/>
if (endstr == -1) <br/>
endstr = document.cookie.length; <br/>
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) { <br/>
var arg = name + "="; <br/>
var alen = arg.length; <br/>
var clen = document.cookie.length; <br/>
var i = 0; <br/>
while (i &lt; clen) { <br/>
var j = i + alen; <br/>
if (document.cookie.substring(i, j) == arg) <br/>
return getCookieVal (j); <br/>
i = document.cookie.indexOf(" ", i) + 1; <br/>
if (i == 0) break; <br/>
} <br/>
return null;
}
function SetCookie (name, value) { <br/>
var argv = SetCookie.arguments; <br/>
var argc = SetCookie.arguments.length; <br/>
var expires = (argc &gt; 2) ? argv[2] : null; <br/>
var path = (argc &gt; 3) ? argv[3] : null; <br/>
var domain = (argc &gt; 4) ? argv[4] : null; <br/>
var secure = (argc &gt; 5) ? argv[5] : false; <br/>
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) + <br/>
((domain == null) ? "" : ("; domain=" + domain)) + <br/>
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) { <br/>
var exp = new Date(); <br/>
exp.setTime (exp.getTime() - 1); <br/>
var cval = GetCookie (name); <br/>
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
Copy linkTweet thisAlerts:
@benzen78authorJul 08.2005 — Cool, sorted it out out now, seems to work fine!! thanks for the help!!!
Copy linkTweet thisAlerts:
@Mr_JJul 08.2005 — I had a look at that site, but that seems different to the way I have done things in the past, it seems a very long way around the problem, the script seems long to what I have done in the past.... [/QUOTE]

No longer than emblems post for the basic cookie functions, it depends on the information the cookie is saving/retrieving as to the length of the other functions
×

Success!

Help @benzen78 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.16,
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,
)...