...

View Full Version : Need Help with setting cookies and assigning to a button



Broll05
12-07-2010, 07:12 PM
I am trying to set my cookies with the click of a button and display them with another button and have them expire one day after visit. I have been having some trouble with this and have written and rewritten code many times. How can I get this code to work? (not very familiar with setting cookies) Any help with this would be greatly appreciated. Thank you.

Below is the cookie script I am working on which I am having trouble with:


<script>
var equipment = document.forms[0].equipment.value;
var pDate = document.forms[0].pickupDate.value;
var pHour = document.forms[0].pickupHours.value;
var pMin = document.forms[0].pickupMinutes.value;
var rDate = document.forms[0].returnDate.value;
var rHour = document.forms[0].returnHours.value;
var rMin = document.forms[0].returnMinutes.value;
var first = document.forms[0].firstName.value;
var last = document.forms[0].lastName.value;
var street = document.forms[0].street.value;
var city = document.forms[0].city.value;
var zip = document.forms[0].zip.value;
var dob = document.forms[0].date.value;

var cookieValue = equipment +"~" + pDate + "~" + pHour + "~" + pMin + "~" + rDate + "~" + rHour + "~" + rMin + "~" + first + "~" + last + "~" + street + "~" + city + "~" + zip + "~" + dob

var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*60*60*1000)); // one day ahead

function setCookie(isName,isValue,dExpires) {
document.cookie = isName + "=" + isValue + ";expires=" + dExpires.toGMTString() + "path=/";
}

function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");

if (startSlice == -1) {return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function dispCookie(isName) {
nValue = getCookie(isName);
alert(nValue);
}

function deleteCookie(isName){
if (getCookie(isName)){
document.cookie = isName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
</script>


This is the input field in the body part of the form:


<input type = "button" value = 'Set Cookies' onclick = "setCookie('anyName','Hello',expDate)">

Philip M
12-07-2010, 07:29 PM
<input type = "button" value = 'Set Cookies' onclick = "setCookie('anyName', cookieValue, expDate)">

When retrieving the cookie, use var paramslist = isValue.split("~");
and then you have an array named paramslist containing all the individual values.

Broll05
12-07-2010, 07:31 PM
Is that all I need? I am getting some errors in my code, it keeps saying that my dExpires is undefined. How can I correct this situation and How can I incorporate another button to display the cookies? Any suggestions?

Philip M
12-07-2010, 07:48 PM
I thought we had covered this in http://www.codingforums.com/showthread.php?t=211231 post #3.

I do not see why you get an error message relating to dExpires. The value of expDate (the expiry date) is passed to the function setCookie(isName,isValue,dExpires) { from onclick = "setCookie('anyName',cookieValue,expDate)". So the value of dExpires is one day ahead of today. Introduce some alerts to try and trace your errors.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum