www.webdeveloper.com
Results 1 to 4 of 4

Thread: Cannot get cookie to work

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    Cannot get cookie to work

    I have been working with this for awhile now and cannot seem to get it right. I have a main page (contact us form) where the user fills in their information, once done they are redirected to another page thanking them. I have been trying to add a cookie on the main page that tells if the user has already filled out the form and send them to a whole new page stating they have already filled the form out. Here is what I have.

    Contact us page
    Code:
    <!DOCTYPE html>
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>Taylor Ambulance - Contact</title>
    <link rel="stylesheet" href="contact.asp_files/reset.css" type="text/css" media="screen">
    <link rel="stylesheet" href="contact.asp_files/960_12_col.css" type="text/css" media="screen">
    <link rel="stylesheet" href="contact.asp_files/taylorAmbulance.css" type="text/css" media="screen">
    <link rel="stylesheet" href="contact.asp_files/navigation.css" type="text/css" media="screen">
    <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->
    
    <script type="text/javascript">
    function MM_validateForm() { //v4.0
      if (document.getElementById){
        var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
        for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
          if (val) { nm=val.name; if ((val=val.value)!="") {
            if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
              if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
            } else if (test!='R') { num = parseFloat(val);
              if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
              if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
                min=test.substring(8,p); max=test.substring(p+1);
                if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
          } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
        } if (errors) alert('The following error(s) occurred:\n'+errors);
        document.MM_returnValue = (errors == '');
    } }
    </script>
    </head>
    <script type="text/javascript" src="cookieRedirect.js"></script>
    <body>
    	<div id="backdrop"></div>
    		<div class="wrapper container_12">
    			<header class="grid_12" id="branding">
    				<h2 class="internet">555-0102 | Dispatch 555-0101</h2>
    			</header>
    			<nav>
    				<ul class="grid_12" id="nav">
    					<li><a href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/Healthcare/Taylor/internet/index.asp">Home</a></li>
    					<li><a href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/Healthcare/Taylor/internet/about.asp">About</a></li>
    					<li><a class="selected" href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/Healthcare/Taylor/internet/contact.asp">Contact</a></li>
    					<li class="right"><a href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/Healthcare/Taylor/intranet/index.asp">Intranet</a></li>
    					<li class="right"><a class="selected" href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/Healthcare/Taylor/internet/index.asp">Internet</a></li>
    				</ul>
    			</nav>
    
    		<div class="grid_12" id="subpage_photo_area">
            <img alt="image of an ambulance" src="contact.asp_files/sub-pages-image.png">
    		</div>
    		<article class="grid_9 push_1 internet" id="subpage_main_content">
    			<h2>Contact</h2>
    			<p class="contactcallout">If you have an emergency, hang up and dial 9 - 1 - 1</p>
    		  <p>To schedule a non-emergency transport, please contact the Dispatcher: 555-0101</p>
    			<p>For all other matters, please contact the Operations Manager: 555-0102			</p>
    			<p>&nbsp;</p>
                <form action="acknowledgement.html" method="get">
                <table width="476" border="3" cellpadding="2" cellspacing="0" class="c3">
    	    <tr valign="baseline">
    <td width="89"><span class="c2">First Name*</span></td>
    <td width="284"><input name="First Name" type="text" id="First Name" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Last Name*</span></td>
    <td><input name="Last Name" type="text" id="Last Name" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Phone Number*</span></td>
    <td><input name="Phone Number" type="text" id="Phone Number" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Email Address*</span></td>
    <td><input name="Email Address" type="text" id="Email Address" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Comments*</span></td>
    <td><textarea name="comments" cols="38,1" id="comments"></textarea></td>
    
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    <form>
    <br>
    <div class="c4">      
      <input type="submit" onClick="MM_validateForm('First Name','','R','Last Name','','R','Phone Number','','RisNum','Email Address','','RisEmail','comments','','R');return document.MM_returnValue" value=" Submit "> <input type="button" value=" Return " onclick="window.location='acknowledgement.html'">
      <input type="reset" value="Reset Form" onClick="resetform()">
    </div>
    
    			<p>Taylor Ambulance Company, Inc.<br>
    			207 S. Hartford St.<br>
    			Kelsey</p>
    		</article>
    				<div class="push"></div>
    		</div>
    		
    		<footer class="footer container_12" id="footer">
    			<p class="push_1"><a href="https://ecampus.phoenix.edu/secure/aapd/CIST/VOP/" title="Virtual Organizations Portal">Virtual Organizations Portal</a><span class="copydivider">|</span> 2010, 2011 Apollo Group, Inc. All Rights Reserved.</p>
    		</footer>
    		
    		
    		<!-- Asynchronous Google Analytics snippet. -->
    	<script src="contact.asp_files/ga.js"></script><script>
    		var _gaq=[['_setAccount','UA-1768756-3'],['_trackPageview'],['_trackPageLoadTime']];
    		(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
    		g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
    		s.parentNode.insertBefore(g,s)}(document,'script'));
    	</script>
    
    	<!-- Prompt IE 6 users to install Chrome Frame. Remove this if you want to support IE 6.
           chromium.org/developers/how-tos/chrome-frame-getting-started -->
    	<!--[if lt IE 7 ]>
        	<script defer src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"></script>
        	<script defer>window.attachEvent('onload',function(){CFInstall.check({mode:'overlay'})})</script>
    	<![endif]-->
    	
    </body></html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,767
    Suggest you post your cookie code rather than your web page code if its a code problem, if you want to move the <script type="text/javascript" src="cookieRedirect.js"></script> so it sits like this in your page and see how thing go...
    HTML Code:
    <script type="text/javascript" src="cookieRedirect.js"></script>
    </head>
    It might make a difference.
    --> 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...

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    This is what I have on the cookieredirect.js page


    Code:
    var RedirectURL = "alreadysubmitted.html"
    
    var DaysToLive = "365";
    
    var CookieName = "HasVisited";
    
    function Action() {
    location.href = RedirectURL;
    }
    
    DaysToLive = parseInt(DaysToLive);
    var Value = 'bypass page next time';
    
    function GetCookie() {
    var cookiecontent = '';
    if(document.cookie.length > 0) {
       var cookiename = CookieName + '=';
       var cookiebegin = document.cookie.indexOf(cookiename);
       var cookieend = 0;
       if(cookiebegin > -1) {
          cookiebegin += cookiename.length;
          cookieend = document.cookie.indexOf(";",cookiebegin);
          if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
          cookiecontent = document.cookie.substring(cookiebegin,cookieend);
          }
       }
    if(cookiecontent.length > 0) { return true; }
    return false;
    }
    
    function SetCookie() {
    var exp = '';
    if(DaysToLive > 0) {
       var now = new Date();
       then = now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000);
       now.setTime(then);
       exp = '; expires=' + now.toGMTString();
       }
    document.cookie = CookieName + '=' + Value + exp;
    return true;
    }
    
    if(GetCookie() == true) { Action(); }
    
    SetCookie();

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,767
    Setting, Getting and Deleting cookies.

    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")
    }
    
    
    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));
    }
    
    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"
    }

    Set a future date like this...

    dateObject = new Date();
    dateObject.setDate(365);

    setCookie(CookieName , Value , dateObject);
    Last edited by \\.\; 11-26-2013 at 04:56 AM.
    --> 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...

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.32084 seconds
  • Memory Usage 2,900KB
  • 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
  • (3)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)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 (73):
  • 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_postinfo_query
  • fetch_postinfo
  • 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
  • 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