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
    5,854
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,854
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,716
    script type="text/javascript" src="ProductInfo.html"
    it looks like you missed not only the class where they talked about cookies ))
    signature under construction

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16432 seconds
  • Memory Usage 2,904KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates