Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Need Help with setting cookies and assigning to a button

    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:
    Code:
    <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:
    Code:
    <input type = "button" value = 'Set Cookies' onclick = "setCookie('anyName','Hello',expDate)">

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <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.
    Last edited by Philip M; 12-07-2010 at 07:32 PM.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Broll05; 12-07-2010 at 07:34 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I thought we had covered this in Need Assistance on How to store Cookies containing user information 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •