School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,477 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,759 people online right now. Registration is fast and FREE... Join Now!




Set Cookie/Get Cookie not working?

 

Set Cookie/Get Cookie not working?, Added cookie to form but doesnt seem to work...

PonyDriver

30 Sep, 2008 - 06:07 PM
Post #1

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 18


My Contributions
Hi Everyone,

Well here I am again, I have my form code all set, thanks to PsychoCoder. I now have to add a cookie to the form so that once the form has been completed and submitted it will tell the user that they already submitted the form. I borrowed PsychoCoder's "cookie snippet" as a place to start, but I am not sure how to get the cookie to validate and return a notification page that I have written. I will post the form coed here:

CODE


<html>
<head>

<!--
    Timothy Shannon
    WEB406 - Week 4
    Individual Assignment
    Complete Change Request - #2
    on Service Request - SR-kf-011
-->
<!--
    Page title
-->
<title>Kudler Fine Foods Specials and Events Mailing List Registration Form</title>


<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;
}
  
<!-- Form validation -->  
  
function submitForm()  
{  
    if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {  
        window.alert("You must enter your first and last names.");  
        return false;  
    }  
    else  
    {  
        if (document.forms[0].address.value == "" && document.forms[0].email.value == "")  
        {  
            window.alert("You must enter either a mailing address or email address.");  
            return false;  
        }  
        else  
        {  
            return true;  
        }  
    }  
}  
</script>
</head>

<form name="request" id="request" action="FormProcessor.html"
onsubmit="return submitForm();"
onreset="return confirmReset();"
enctype="application/x-www-form-urlelncoded">

<table border="0" cellspacing="0" cellpadding="5" width="620" >

<thead style="font-family: times-new roman">

<!--
    Header and logo
-->

<tr>
   <td valign="top" width="180" rowspan="2">
      <img src="Kudler.jpg" alt="kudler" width="375" height="200" >
   </td>
   <td style="font-size: 2.2em; color: black; font-weight: bold; font-style: italic"
       valign="top" align="center" >
         Kudler Fine Foods Specials and Events Mailing List Registration
   </td>
</tr>
</thead>

<!-- Customer information header -->
<tr>
   <td style="font-family: times-new roman; font-size: large; font-weight: bold"
       colspan="2" width="100%">
         Customer Information
   </td>
</tr>
<tr>
   <td style="font-family: times-new roman; color: red; font-size: small; font-style: italic"
       colspan="2" width="100%">
         * Required
         ** Either is required
   </td>
</tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>


<!-- Input fields -->
<tr>  
   <td valign="top" colspan="2">
   <table>
    <tr>
        <td width="50"><label for="fname">First</label></td>
        <td><input type="text" name="fname" id="fname" size="30" ><label style="color: red">*</label></td>
        <td width="50"><label for ="lname">Last</label></td>
        <td><input type="text" name="lname" id="lname" size="30" ><label style="color: red">*</label></td>
    </tr>
    <tr>
        <td width="100"><label for ="address">Address</label></td>
        <td><input type="text" name="address" id="address" size="60" ><label style="color: red">**</label></td>
    </tr>
    <tr>
    <td><label for="phone">Phone</label></td>
        <td><input type="text" name="phone" id="phone" size="12" >
    </td>
    </tr>
    <tr>
        <td><label for="email">Email Address</label></td>
        <td><input type="text" name="email" id="email" size="40" ><label style="color: red">**</label></td>
    </tr>
    
    </table>
   </td>
    <tr>
        <td valign="top">
        <input type="checkbox" name="Purpose" id="mail" value="Mailing list" >
        <label for="mail">Add me to your Mailing List</label><br >
        </td>
    </tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>
<!--
Customer comments
-->
<tr>
<td width="100" valign="top">
<label for="comments">Comments</label>
</td>
<td valign="top">
<textarea name="comments" id="comments" rows="6" cols="40">
</textarea>
</td>
</tr>

<!-- Register/cancel buttons -->
<tr>
   <td valign="top" colspan="2" align="center">
   <input type="Submit" value="Register" >
   <input type="reset" value="Cancel" >
   </td>
  
</tr>

<!-- Company name and locations -->

<tr style="background-color: red">
   <td colspan="2" align="center" valign="bottom">
      <address style="font-family: times-new roman; font-size: 0.6em; font-style: normal; font-size: small; color: black">
      Kudler Fine Foods · Shopping the World for the Finest Foods · La Jolla · Del Mar · Encinitas ·
      </address>
   </td>
</tr>
</table>


</form>

</html>



And I will post the notification page code here:

CODE


<html>
<head>
<title>Form Already Processed</title>
</head>
<body>
<script type="text/javascript">
document.write("<h1>Your information is already on file!</h1><h2>You have already registered for our list!</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 />");
}
</script>
</body>
</html>



Thank you all again in advance for any help offered, I have received nothing but help here in the past and I've got to tell you it's a GREAT feeling having somewhere you can go for help and actually get answers.

Tim

User is offlineProfile CardPM
+Quote Post


PonyDriver

RE: Set Cookie/Get Cookie Not Working?

1 Oct, 2008 - 08:36 AM
Post #2

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 18


My Contributions
Hey Everyone,

I just don't seem to grasp the cookie thing and I don't understand why. I've read the text multiple times read the tutorial here and it just doesn't sink in. Please if someone could just give me some direction, like what am I missing, do I have to read the cookie at some point and do I have to tie it in to the onsubmit call? I could barely sleep last night thinking about this, and I woke up thinking about it, help!!!

Tim
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Set Cookie/Get Cookie Not Working?

1 Oct, 2008 - 08:56 AM
Post #3

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Sorry for the delay, I work alot. Take a look at these modifications, I added comments to what I did to try and help you understand what Im doing

jscript

function submitForm()
{
//check and see if the cookie has been set yet
if(!get_cookie("FormSubmitted") == "yes")
{
if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {
window.alert("You must enter your first and last names.");
return false;
}
else
{
if (document.forms[0].address.value == "" && document.forms[0].email.value == "")
{
window.alert("You must enter either a mailing address or email address.");
return false;
}
else
{
//now lets set the cookie. I'm creating a cookie
//named FormSubmitted with a value of "yes" that
//lasts 30 days
set_cookie("FormSubmitted", "yes", 30)
return true;
}
}
}
else
{
//in this example Im just alerting the user
//you can redirect or do it how you need to
alert("You've already submitted this once before");
}
}

User is offlineProfile CardPM
+Quote Post

PonyDriver

RE: Set Cookie/Get Cookie Not Working?

1 Oct, 2008 - 11:29 AM
Post #4

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 18


My Contributions
Hi PsychoCoder,

Thank you for your response and your suggested changes, I wasn't sure if I should just make the changes you indicated to the code I posted above or if I should start with my original code (without your snippets added). So I added the suggestions you made to the original code (without your snippets) and now the form submits without validating the fields and I can keep submitting the form so I'm not sure if the cookie is being set. I have been trying to figure out what is causing the form to just submit without doing the validation but to no avail, I'm still stuck! I followed your comments and I understand what you have done to add the cookie but somewhere something is causing the code not to do the validation. If you have a chance to take another look I would really appreciate it, I will copy the code as I have it now here:

CODE


<html>
<head>

<!--
    Timothy Shannon
    WEB406 - Week 4
    Individual Assignment
    Complete Change Request - #2
    on Service Request - SR-kf-011
-->
<!--
    Page title
-->
<title>Kudler Fine Foods Specials and Events Mailing List Registration Form</title>


<script type="text/javascript">  
  
<!--
    Form validation
-->  
  
function submitForm()  
{  
    if(!get_cookie("FormSubmitted") == "yes")
    {
        if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {  
        window.alert("You must enter your first and last names.");  
        return false;  
        }  
        else  
        {  
            if (document.forms[0].address.value == "" && document.forms[0].email.value == "")  
            {  
                window.alert("You must enter either a mailing address or email address.");  
                return false;  
            }  
            else  
            {
                set_cookie("FormSubmitted", "yes", 30)
                return true;  
            }  
        }  
    }
    else    
    {
        alert("You've already submitted this once before");
    }
}  

</script>
</head>

<form name="request" id="request" action="FormProcessor.html"
onsubmit="return submitForm();"
onreset="return confirmReset();"
enctype="application/x-www-form-urlelncoded">

<table border="0" cellspacing="0" cellpadding="5" width="620" >

<thead style="font-family: times-new roman">

<!--
    Header and logo
-->

<tr>
   <td valign="top" width="180" rowspan="2">
      <img src="Kudler.jpg" alt="kudler" width="375" height="200" >
   </td>
   <td style="font-size: 2.2em; color: black; font-weight: bold; font-style: italic"
       valign="top" align="center" >
         Kudler Fine Foods Specials and Events Mailing List Registration
   </td>
</tr>
</thead>

<!-- Customer information header -->
<tr>
   <td style="font-family: times-new roman; font-size: large; font-weight: bold"
       colspan="2" width="100%">
         Customer Information
   </td>
</tr>
<tr>
   <td style="font-family: times-new roman; color: red; font-size: small; font-style: italic"
       colspan="2" width="100%">
         * Required
         ** Either is required
   </td>
</tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>


<!-- Input fields -->
<tr>  
   <td valign="top" colspan="2">
   <table>
    <tr>
        <td width="50"><label for="fname">First</label></td>
        <td><input type="text" name="fname" id="fname" size="30" ><label style="color: red">*</label></td>
        <td width="50"><label for ="lname">Last</label></td>
        <td><input type="text" name="lname" id="lname" size="30" ><label style="color: red">*</label></td>
    </tr>
    <tr>
        <td width="100"><label for ="address">Address</label></td>
        <td><input type="text" name="address" id="address" size="60" ><label style="color: red">**</label></td>
    </tr>
    <tr>
    <td><label for="phone">Phone</label></td>
        <td><input type="text" name="phone" id="phone" size="12" >
    </td>
    </tr>
    <tr>
        <td><label for="email">Email Address</label></td>
        <td><input type="text" name="email" id="email" size="40" ><label style="color: red">**</label></td>
    </tr>
    
    </table>
   </td>
    <tr>
        <td valign="top">
        <input type="checkbox" name="Purpose" id="mail" value="Mailing list" >
        <label for="mail">Add me to your Mailing List</label><br >
        </td>
    </tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>
<!--
Customer comments
-->
<tr>
<td width="100" valign="top">
<label for="comments">Comments</label>
</td>
<td valign="top">
<textarea name="comments" id="comments" rows="6" cols="40">
</textarea>
</td>
</tr>

<!-- Register/cancel buttons -->
<tr>
   <td valign="top" colspan="2" align="center">
   <input type="Submit" value="Register" >
   <input type="reset" value="Cancel" >
   </td>
  
</tr>

<!-- Company name and locations -->

<tr style="background-color: red">
   <td colspan="2" align="center" valign="bottom">
      <address style="font-family: times-new roman; font-size: 0.6em; font-style: normal; font-size: small; color: black">
      Kudler Fine Foods · Shopping the World for the Finest Foods · La Jolla · Del Mar · Encinitas ·
      </address>
   </td>
</tr>
</table>


</form>

</html>




Once again thank you in advance for your help, I need to turn in the assignment tonight, so I am hoping you will come to my rescue. :-)

Tim
User is offlineProfile CardPM
+Quote Post

PonyDriver

RE: Set Cookie/Get Cookie Not Working?

1 Oct, 2008 - 07:59 PM
Post #5

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 18


My Contributions
Aaaaarrrrrggggghhhhhhhh! Something isn't right, I can't even get the form to validate even when I cut and paste the original form validation code in place of the set cookie/form validation code that was suggested, and I'm fried and bleary eyed! Thank you PsychoCoder for all your help you have been great, if you do get a chance to take another peek at this I would love to find out why I couldn't get it to work, that will be a real learning experience, talk about learning from mistakes. Is 49 too old to be switching careers and trying to become a programmer?

Thanks again PsychoCoder.

Tim
User is offlineProfile CardPM
+Quote Post

R Power

RE: Set Cookie/Get Cookie Not Working?

9 Oct, 2008 - 01:51 AM
Post #6

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 4


My Contributions
Here is the complete code for setcookie, get cookie and Ckeckcookie.
CODE

<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
  else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}
</script>
</head>

<body onLoad="checkCookie()">
</body>
</html>


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:36AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month