Solved

Cannot get cookie to work

Posted on 2013-11-24
10
425 Views
Last Modified: 2014-01-01
I have been working on this for days and cannot seem to get the cookie to work. I need to place a cookie on the users system so when they fill the form out again it will redirect them to another page stating they already filled the form out. I cannot seem to get it to work.

<!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="../week2/contact.asp_files/reset.css" type="text/css" media="screen">
<link rel="stylesheet" href="../week2/contact.asp_files/960_12_col.css" type="text/css" media="screen">
<link rel="stylesheet" href="../week2/contact.asp_files/taylorAmbulance.css" type="text/css" media="screen">
<link rel="stylesheet" href="../week2/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">
<!-- Define cookie -->
	 
	set_cookie("LoggedIn",true,10);
	 
	function set_cookie(fname, value, days)
	{
	   if (days)
	   {
	      var date = new Date();
	      date.setTime(date.getTime() + (days*24*60*60*1000));
	      var expires = "; expires=" + date.toGMTString();
	   } else var expires = "";
	    
	   var host = "your_domain.com";
	   var domain = "; domain=" + host;
	   document.cookie = fname + "=" + value + expires + domain + "; path=/";
	}
	 
	get_cookie("LoggedIn");
	 
	function get_cookie(fname)
	{
	   var EQ = fname + "=";
	   var ca = document.cookie.split(';');
	   for (var i=0;i < ca.length;i++)
	   {
	      var c = ca[i];
	      while (c.charAt(0)==' ') c = c.substring(1,c.length);
	      if (c.indexOf(EQ) == 0) return c.substring(EQ.length,c.length);
    }
       return null;
	} 
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>
<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="../week2/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="../week2/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>

Open in new window

0
Comment
Question by:redrose43311
  • 4
  • 3
10 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 334 total points
Comment Utility
"Chrome Frame" is being discontinued.

I would read the cookie on the form page after setting it on the 'action' page.  I assume the 'action' page, "acknowledgement.html", will become an ASP or PHP page when you're done.

Javascript cookie routines are shown here: http://www.w3schools.com/js/js_cookies.asp
0
 

Author Comment

by:redrose43311
Comment Utility
I have the acknowledgement.html done and it works. Just not the cookie. Here is the code for the acknowledgement.html page.

<!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="../../TaylorVOFiles/TaylorVOFiles/contact.asp_files/reset.css" type="text/css" media="screen">
<link rel="stylesheet" href="../../TaylorVOFiles/TaylorVOFiles/contact.asp_files/960_12_col.css" type="text/css" media="screen">
<link rel="stylesheet" href="../../TaylorVOFiles/TaylorVOFiles/contact.asp_files/taylorAmbulance.css" type="text/css" media="screen">
<link rel="stylesheet" href="../../TaylorVOFiles/TaylorVOFiles/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]-->

</head>
<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="../../TaylorVOFiles/TaylorVOFiles/contact.asp_files/sub-pages-image.png">
		</div>
		<article class="grid_9 push_1 internet" id="subpage_main_content">
			<h2>Thank You For Contacting Taylor Ambulance Company, Inc</h2>
 <script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
if (window != top)
 top.location.href=location.href
document.write("<h1>Your form has been submitted!</h1><h2>You entered the following data:</h2>");

var formData = location.search;
formData = formData.substring(1, formData.length);

while (formData.indexOf("+") != -1) {
 formData = formData.replace("+", " ");
}

formData = unescape(formData);
var formArray = formData.split("&");

for (var i=0; i < formArray.length; ++i) {
 document.writeln(formArray[i] + "<br />");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

  
			
		  <p>&nbsp;</p>
          


		  <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="../../TaylorVOFiles/TaylorVOFiles/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>

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Are you running this thru a web server or just from a file?  Without a web server, you have no 'domain' to set the cookie on.  Chrome won't even try while IE and Firefox might try but without a 'domain', they won't be able to read it.
0
 

Author Comment

by:redrose43311
Comment Utility
This is an assignment and will be ran from a file
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 334 total points
Comment Utility
Can't work then.  For cookies to work, the page Must be accessed thru a web server so there will be a 'domain' for the cookie even if it is only an IP address.
0
 

Author Comment

by:redrose43311
Comment Utility
Im confused. I load the page through firefox while on my computer and my instructor loads the pages on his system.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Cookies require a 'domain' even if it is only an IP address thru a web server.  You can only read cookies on the "current domain".  If you load the file directly, there is no 'domain' so cookies can't be read.  See here: http://en.wikipedia.org/wiki/HTTP_cookie  Chrome won't even let you set or read a cookie on 'localhost'.
0
 
LVL 4

Assisted Solution

by:smeghammer
smeghammer earned 166 total points
Comment Utility
Hi,

If you are using a student machine, it probably has a webserver already (IIS or Apache). On Windows, the IIS web root path defaults to

c:\inetpub\wwwroot\.

Put your HTML file in there and call it in the browser like so:

http://localhost/[yourfile].htm

That way, you will have the domain set to localhost, and the cookies should work. You will also need to edit the javascript to ensure the domain added to the cookie is the correct one (line 28 of your javascript).

Your instructor is likely calling his localhost, or perhaps an internal domain/IP address.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now