www.webdeveloper.com
Results 1 to 5 of 5

Thread: how to set cookies

  1. #1
    Join Date
    Sep 2013
    Posts
    26

    how to set cookies

    I have a question. I am trying to make a document form that sets car rental reservations. I need to make a button that lets the user see a form that they have already filled out. The cookies are supposed to keep for one day. My first question is do I put the code for the cookies on another js page? Here is my code so far. I have been sick and I missed the class where they talked about this. Any help would be great. Thank You

    Code:
    <head>
    <title>CarRental</title>
    <meta http-equiv="content-type" content="text/html;
    charset=iso-8859-1" />
    <link rel="STYLESHEET" type="text/css" HREF="/Stylesheets/Citrus.css">
    <script type="text/javascript">
    </script>
    </head>
    <body>
    <h1>Car Rental</h1>
    <h2>Reservations</h2>
    <form action=""method="get" enctype="application/x-www-form-urlencoded">
    <p><input type="button" name="Show" value=
    " Show" onclick="nextForm()" /> an existing reservation or create a new one below.</p>
    <h2>Name and Address</h2>
    <p>First Name
    <input type="text" name="name" size="30" />
    Last Name
    <input type="text" name="name" size="30" /></p>
    <p>Street Address 1
    <input type="text" name="address" size="50" /></p>
    <p>Street Address 2
    <input type="text" name="address" size="50" /></p>
    <p>City
    <input type="text" name="city" size="25" />
    State <input type="text"
    name="state" size="5" maxlength="2" />
    Zip <input type="text" name=
    "zip" size="15" maxlength="5" /></p>
    <p>Phone
    <input type="text" name="Phone" size="20" />
    Fax
    <input type="text" name="Fax" size="20" /></p>
    <h2>Reservation Request</h2>
    <p>Pickup Date
    <select name="month">
    <option value="Jan">Jan</option>
    <option value="Feb">Feb</option>
    <option value="Mar">Mar</option>
    <option value="Apr">Apr</option>
    <option value="May">May</option>
    <option value="Jun">Jun</option>
    <option value="Jul">Jul</option>
    <option value="Aug">Aug</option>
    <option value="Sep">Sep</option>
    <option value="Oct">Oct</option>
    <option value="Nov">Nov</option>
    <option value="Dec">Dec</option>
    </select>
    <select name="Day">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
    </select>
    <select name="Year">
    <option value="2009">2009</option>
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012">2012</option>
    <option value="2013">2013</option>
    </select></p>
    <p>Return date
    <select name="month">
    <option value="Jan">Jan</option>
    <option value="Feb">Feb</option>
    <option value="Mar">Mar</option>
    <option value="Apr">Apr</option>
    <option value="May">May</option>
    <option value="Jun">Jun</option>
    <option value="Jul">Jul</option>
    <option value="Aug">Aug</option>
    <option value="Sep">Sep</option>
    <option value="Oct">Oct</option>
    <option value="Nov">Nov</option>
    <option value="Dec">Dec</option>
    </select>
    <select name="Day">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
    </select>
    <select name="Year">
    <option value="2009">2009</option>
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012">2012</option>
    <option value="2013">2013</option>
    </select></p>
    <h3>Pick the type of car you want!</h3>
    <select name="Car Type">
    <option value="car">Car</option>
    <option value="truck">Truck</option>
    <option value="van">Van</option>
    <option value="toyota">Toyota</option>
    <option value="honda">Honda</option>
    <option value="chevy">Chevy</option>
    <option value="bmw">BMW</option>
    <option value="dodge">Dodge</option>
    </p>
    <p><input type="button" name="Submit Reservation" value=
    " Submit Reservation" onclick="registerForm()" /></p>
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Sep 2013
    Posts
    26
    I just changed <link rel="STYLESHEET" type="text/css" HREF="/Stylesheets/Citrus.css"> to this
    <script type="text/javascript" src="Cookie.js"></script> and I am now trying to figure things out but so far this is what I have on the js page. Am I on track or what?

    function registerForm()











    function setCookie(name, value, seconds)
    {
    var cookie = name + "=" + encodeURIComponent(value);
    var time = parseInt(seconds);

    if (!isNaN(time))
    {
    cookie += "; max-age=" + time;
    } //else, time is invalid - create session cookie

    document.cookie = cookie + "; path=/";
    }

    function getCookie(name)
    {
    var cookies = parseCookies();
    return cookies[name];
    }

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    You save cookies on one page and retrieve them on the next page. Cookies are not page specific.

    Set a Cookie
    Code:
    function setCookie(name, value, expires, path, domain, secure){
    	if (!expires){expires = new Date()}
    	document.cookie = name + "=" + escape(value) + 
    		((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    		((path == null) ? "" : "; path=" + path) +
    		((domain == null) ? "" : "; domain=" + domain) +
    		((secure == null) ? "" : "; secure")
    }

    Retrieve a Cookie
    Code:
    function getCookie(name) {
    	var arg = name + "=", alen = arg.length, clen = document.cookie.length, i = 0
    	while (i < clen) {
    		var j = i + alen
    		if (document.cookie.substring(i, j) == arg){
    			return getCookieVal(j)
    			}
    		i = document.cookie.indexOf(" ", i) + 1
    		if (i == 0) break
    	}
    return null
    }
    
    function getCookieVal(offset){
    	var endstr = document.cookie.indexOf (";", offset);
    	if (endstr == -1)
    		endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }
    Delete a Cookie
    Code:
    function deleteCookie(name,path,domain){
    	document.cookie = name + "=" +
    		((path == null) ? "" : "; path=" + path) +
    		((domain == null) ? "" : "; domain=" + domain) +
    		"; expires=Thu, 01-Jan-00 00:00:01 GMT"
    }

    So you can have all those function in one JS file that then gives you the ability to set, get and delete a cookie.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    You will note that toGMTString() method is depreciated, nothing as far as I am aware has replaced it for the purpose of setting and dealing with cookies. If anyone has a work-around then please cough up.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    script type="text/javascript" src="ProductInfo.html"
    it looks like you missed not only the class where they talked about cookies ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles