0

I am trying to save cookies in JavaScript and then display them on the page again but for some reason my setCookies function and displayCookies function is not working properly.. I want it to also expire after 1 week. It is coming from a form and when I put it in Web Developer in FireFox it says Reference Error setCookies and displayCookies is not defined. My jsfiddle is : http://jsfiddle.net/FmXJW/

    function setCookies()
    {
        // this function will set cookies for each field in the reservation form, and set them to expire after one day
        var Name = document.forms[0].txtName.value;
        var Address = document.forms[0].txtAddress.value;
        var City = document.forms[0].txtCity.value;
        var State = document.forms[0].txtState.value;
        var Zip = document.forms[0].txtZip.value;
        var Email = document.forms[0].txtEmail.value;
        var CarType = document.forms[0].txtCarType.value;
        var PickupDate = document.forms[0].txtPickupDate.value;
        var ReturnDate = document.forms[0].txtReturnDate.value;

        var myDate = newDate();
        myDate.setDate(myDate.getDate() + 7);

        document.cookie = "name=" + encodeURIComponent(Name) + "; expires=" + myDate.toUTCString();
        document.cookie = "address=" + encodeURIComponent(Address) + "; expires=" + myDate.toUTCString();
        document.cookie = "city=" + encodeURIComponent(City) + "; expires=" + myDate.toUTCString();
        document.cookie = "state=" + encodeURIComponent(State) + "; expires=" + myDate.toUTCString();
        document.cookie = "zip=" + encodeURIComponent(Zip) + "; expires=" + myDate.toUTCString();
        document.cookie = "email=" + encodeURIComponent(Email) + "; expires=" + myDate.toUTCString();
        document.cookie = "carType=" + encodeURIComponent(CarType) + "; expires=" + myDate.toUTCString();
        document.cookie = "pickupDate=" + encodeURIComponent(PickupDate) + "; expires=" + myDate.toUTCString();
        document.cookie = "returnDate=" + encodeURIComponent(ReturnDate) + "; expires=" + myDate.toUTCString();

        window.alert ("Your reservation has been saved.");

    } // end function setCookies()

    function displayCookies()
    {
        // this function will read the saved cookies, and repopulate the form with the cookie values

        var cookieString = decodeURIComponent(document.cookie);
        var cookieArray = cookieString.split("; ");

        if (document.cookie == 0)
        {
            alert("You have not made a reservation");
        }
        else
        {
            // retrieve each cookie, and display the cookie value in the appropriate form field
        document.forms[0].txtName.value = cookieArray[0].lastIndexOf("=") + 1);
        document.forms[0].txtAddress.value = cookieArray[1].lastIndexOf("=") + 1);
        document.forms[0].txtCity.value = cookieArray[2].lastIndexOf("=") + 1);
        document.forms[0].txtState.value = cookieArray[3].lastIndexOf("=") + 1);
        document.forms[0].txtZip.value = cookieArray[4].lastIndexOf("=") + 1);
        document.forms[0].txtEmail.value = cookieArray[5].lastIndexOf("=") + 1);
        document.forms[0].txtCarType.value = cookieArray[6].lastIndexOf("=") + 1);
        document.forms[0].txtPickupDate.value = cookieArray[7].lastIndexOf("=") + 1);
        document.forms[0].txtReturnDate.value = cookieArray[8].lastIndexOf("=") + 1);
        }

    } // end function displayCookies()
  • You say your function is not working properly but do not say _what_ is the problem. Could you be a bit more specific? – excentris Apr 10 '13 at 06:38
  • Yes sorry, it is not creating the cookie at all.. and when I put it in web developer on firefox is says Reference error setCookies is not defined and the same for displayCookies – Jonny Parko Apr 10 '13 at 06:44
  • Where are you calling those functions? Could you maybe provide a [jsfiddle](http://jsfiddle.net/) reproducing your problem? – excentris Apr 10 '13 at 06:47
  • I am calling it in the Save Reservation button in the form.. Sure here is the jsfiddle.. http://jsfiddle.net/FmXJW/ – Jonny Parko Apr 10 '13 at 06:50

1 Answers1

0

You have a couple of problems on your code:

You are not retrieving the value from the different fields stored on the cookie correctly, you need to use slice to get the right part.

I also guess the line var myDate = newDate(); should be var myDate = new Date();.

I have changed your fiddle (find it here) and loaded the code in <head> and now it seems to save and display the cookies correctly.

excentris
  • 471
  • 1
  • 7
  • 25