Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    A little cookie help

    Hi im new to working with cookies so would appreaciate a little help

    I using the w3c tuturial as a template so heres a link so you can see what im trying to do http://www.w3schools.com/JS/js_cookies.asp

    Code:
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
      {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function setCookie(c_name,value,exdays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    }
    
    function checkCookie(username)
    {
    var username=getCookie("username");
    if (username!=null && username!="")
      {
      document.getElementById("feedback").innerHTML = " last time you scored " + username);
      setCookie("username",username,365);
      }
      
    else 
      {
        if (username=null || username="")
        {
        setCookie("username",username,365);
        }
      }
    }
    I kept the variable names the same so you can follow and I don't confuse myself and make things more complected while im trying to debug it.

    Some extra information. checkcookie is been given a variable it is just a simple number. Im making a questionnaire which is done but I wont the top of the page to tell the user how much they scored last or tell them this is their first time trying it. The variable being passed to check cookie is their score .

    at the moment when I click the submit button nothing happens. Where it should trigger a score calculating function which should then call the checkcookie function while passing the score it calculated.


    I would love if someone can point me in the right direction or help me correct it or atleast explain to me whats going wrong. Thanks and a lots of appreciation if anyone can spare the time

  2. #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    function checkCookie(username)
    {
    var username=getCookie("username");
    if (username!=null && username!="")
      {
      document.getElementById("feedback").innerHTML = " last time you scored " + username);
      setCookie("username",username,365);
      }
      
    else 
      {
        if (username=null || username="")
        {
        setCookie("username",username,365);
        }
      }
    }

    That should be if (username == null || username == "") although it's a redundant test because you have already tested the inverse.

    You need to show all your code, so it's clear how your functions are being called.

  3. #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your reply I commented out the if statement for a start, here is my full code



    Code:
    <html>
    <head>
    <title> The Internet Blog - Questionaire</title>
    <link href="css/Main.css" rel="stylesheet" type="text/css" >
    
    <style type="text/css">
    .formholder{
    text-align:center;
    float:left;
    marrgin-bottom;15px;
    margin-left:100px;
    }
    
    .contentbox {
    height:950px;
    }
    
    .questions {
    font-weight:bold;
    color:#ff1122;
    
    }
    
    
    td {
        padding-bottom: 10px;
    	padding-left: 10px;
    }
    
    </style>
    
    <script type="text/javascript">
    function results()
    {
    var score=0;
    
    if (document.forms.Questionaire.group1[2].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group2[0].checked && document.forms.Questionaire.group2[1].checked && document.forms.Questionaire.group2[4].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group3[1].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group4.value =="w3c" || document.forms.Questionaire.group4.value =="W3c" || document.forms.Questionaire.group4.value =="w3C" || document.forms.Questionaire.group4.value =="W3C")
    {
    score++;
    }
    
    if (document.forms.Questionaire.group5[1].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group6[2].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group7[15].selected)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group8[2].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group9[1].checked)
    {
    score++;
    }
    
    if (document.forms.Questionaire.group9[1].checked)
    {
    score++;
    }
    
    var texts="feedback";
    if (score < 5)
    {
    texts="You scored";
    }else{
    texts="Well done you scored";
    }
    
    var history="";
    if (score < 5)
    {
    texts="You scored";
    }else{
    texts="Well done you scored";
    }
    
       
    checkCookie(score);
    	
    	
    	
    	newWindow = window.open("")
        newWindow.focus()
        var newContent = "<HTML><HEAD><TITLE>Results</TITLE></HEAD>"
        newContent += "<BODY BGCOLOR='#FFFFFF'><H1>Below you can see how well you did</H1>"
        newContent += texts + " " + score;
    	newContent += " out of ten ";
    	newContent += "</BODY></HTML>"
    	
    	
    	
        newWindow.document.write(newContent)
        newWindow.document.close()
    	
    	
    
    }
    
    
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
      {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function setCookie(c_name,value,exdays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    }
    
    function checkCookie(username)
    {
    
    //alert(username);
    
    var username=getCookie("username");
    if (username!=null && username!="")
      {
      document.getElementById("feedback").innerHTML = " last time you scored " + username);
      setCookie("username",username,365);
      }
      
    else 
      {
       // if (username=null || username="")
        //{
        setCookie("username",username,365);
       // }
      }
    }
    
    </script>
    
    </head>
    <body >
    
    <div class="topbar"></div>
    
    <div class="navbar">
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="Articles.html">Articles</a></li> 
      <li><a class="current" href="Questionaire.html">Questionaire </a></li>  
      <li><a href="Contact.html">Contact </a></li>
      
      
      
      </ul>
     </div>
      <div class ="thedate">
     <SCRIPT>
    document.write(Date()+".")
    </SCRIPT>
    </div>
    <div class="contentbox"> <p style="text-align:center;">Hello and Welcome to the internet blog <br>
    I hope you have read and learnt everything her is the questionaire where we will ask questions about the articles on the site
    <br>
    <br>
    
    <br>
    Have a go below 
    <div id="feedback">its your first time </div>
    <br>
    <br>
    <br>
    
    
     <table>
     <form name="Questionaire" action="javascript:results()"   method="post" enctype="text/plain"> <!--onsubmit="return results()"-->
       <tr>
       <td colspan="6"> 
          <span class="questions"> How many canidates where their in website of the year? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">10         :<input type="radio" name="group1" value="1"></td>
       <td colspan="2">100        :<input type="radio" name="group1" value="2"></td>
       <td colspan="2">255        :<input type="radio" name="group1" value="3"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What three things did the voters on? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td>Content         :<input type="checkbox" name="group2" value="content"></td>
       <td>Navigation      :<input type="checkbox" name="group2" value="navigation"></td>
       <td>Code clarity    :<input type="checkbox" name="group2" value="code"></td>
       <td>Number of users :<input type="checkbox" name="group2" value="users"></td>
       <td>Design          :<input type="checkbox" name="group2" value="design"></td>
       <td>Number of pages :<input type="checkbox" name="group2" value="pages"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What does HTML stand for? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">High Text Markup Language         :<input type="radio" name="group3" value="High Text Markup Language "></td>
       <td colspan="2">Hyper Text Markup Language         :<input type="radio" name="group3" value="Hyper Text Markup Language"></td>
       <td colspan="2">Hyper Text Makeup  Language        :<input type="radio" name="group3" value="Hyper Text Makeup Language"></td>
       </tr>
     
     
      <tr>
       <td colspan="6"> 
        <span class="questions">Who maintains HTML? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="6">(non-case sensitive)         :<input type="text" name="group4" value="Enter Answer Here" size="20" ></td>
       </tr>
      
       <tr>
       <td colspan="6"> 
        <span class="questions">When did the internet start (In its most primitive form)? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">40's         :<input type="radio" name="group5" value="40's"></td>
       <td colspan="2">50's         :<input type="radio" name="group5" value="50's"></td>
       <td colspan="2">60's       :<input type="radio" name="group5" value="60's"></td>
       </tr>
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What Network Led the development of the Internet? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">Stormbreaker        :<input type="radio" name="group6" value="stormbreaker"></td>
       <td colspan="2">Titanus-Connect         :<input type="radio" name="group6" value="titanus"></td>
       <td colspan="2">ARPANET       :<input type="radio" name="group6" value="arpanet"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">when was it commercialised? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="6">Choose the Year         :<select name="group7">
    <option value="1980">1980</option>
    <option value="1981">1981</option>
    <option value="1982">1982</option>
    <option value="1983">1983</option>
    <option value="1984">1984</option>
    <option value="1985">1985</option>
    <option value="1986">1986</option>
    <option value="1987">1987</option>
    <option value="1988">1988</option>
    <option value="1989">1989</option>
    <option value="1990">1990</option>
    <option value="1991">1991</option>
    <option value="1992">1992</option>
    <option value="1993">1993</option>
    <option value="1994">1994</option>
    <option value="1995">1995</option>
    <option value="1996">1996</option>
    <option value="1997">1997</option>
    <option value="1998">1998</option>
    <option value="1999">1999</option>
    </select></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">what was removed to remove the restriction on use of the internet? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">End of the cold war        :<input type="radio" name="group8" value="End of the cold war"></td>
       <td colspan="2">A Patent         :<input type="radio" name="group8" value="A Patent"></td>
       <td colspan="2">NSFNET       :<input type="radio" name="group8" value="NSFNET"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">Are all networks accessible?</span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="3">Yes        :<input type="radio" name="group9" value="Yes"></td>
       <td colspan="3">NO         :<input type="radio" name="group9" value="No"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What is the Internet made of?</span> 
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="3">A Lot of  Large Networks  interconnected      :<input type="radio" name="group10" value="Large Networks"></td>
       <td colspan="3">A lot of small and large networks interconnected         :<input type="radio" name="group10" value="Small and large networks"></td>
       </tr>
       
       
      <tr> 
        <td colspan="3">  <INPUT TYPE="reset"></td>
    	<td colspan="3">  <INPUT TYPE="submit" Value="submit"></td>
      </tr>	
     </FORM> 
     </table>
    
    
    
    
    <br>
    <br>
    
    
    
     </p></div>
    
    <div class="tablepos">
    	<table font="verdana" margin-top="100px" align="right" border="1" bordercolor="#333333" style="background-color:#666666" width="180" cellpadding="3" cellspacing="0">
    	<tr>
    		<td><a href="articles.html">Articles</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/About.html">About the Net</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/History.html">History of the Net</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/HTML.html">HTML code</a></td>
    	</tr>
    	<tr>
    		<td><a href="articles/Arsenal.html">Arsenal </a></td>
    	</tr>
    </table> 
    
    </div>
    
    
    
    <div class="copyfooter"> </div>
    
    
    </body>
    </html>

  4. #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    The error console shows a syntax error.

    The setting and checking of the cookie should be separated, so that the saved value can be displayed on load. This now works, but opening a new window isn't the best way to show the result.

    Code:
    <html>
    <head>
    <title> The Internet Blog - Questionaire</title>
    <link href="css/Main.css" rel="stylesheet" type="text/css" >
    
    <style type="text/css">
    .formholder{
    text-align:center;
    float:left;
    marrgin-bottom;15px;
    margin-left:100px;
    }
    
    .contentbox {
    height:950px;
    }
    
    .questions {
    font-weight:bold;
    color:#ff1122;
    
    }
    
    
    td {
        padding-bottom: 10px;
    	padding-left: 10px;
    }
    
    </style>
    
    <script type="text/javascript">
    function results( theForm )
    {
    var score=0;
    
    if (theForm.group1[2].checked)
    {
    score++;
    }
    
    if (theForm.group2[0].checked && theForm.group2[1].checked && theForm.group2[4].checked)
    {
    score++;
    }
    
    if (theForm.group3[1].checked)
    {
    score++;
    }
    
    if (theForm.group4.value =="w3c" || theForm.group4.value =="W3c" || theForm.group4.value =="w3C" || theForm.group4.value =="W3C")
    {
    score++;
    }
    
    if (theForm.group5[1].checked)
    {
    score++;
    }
    
    if (theForm.group6[2].checked)
    {
    score++;
    }
    
    if (theForm.group7[15].selected)
    {
    score++;
    }
    
    if (theForm.group8[2].checked)
    {
    score++;
    }
    
    if (theForm.group9[1].checked)
    {
    score++;
    }
    
    if (theForm.group9[1].checked)
    {
    score++;
    }
    
    var texts="feedback";
    if (score < 5)
    {
    texts="You scored";
    }else{
    texts="Well done you scored";
    }
    
    var history="";
    if (score < 5)
    {
    texts="You scored";
    }else{
    texts="Well done you scored";
    }
    
    
    // checkCookie(score);
    	
       setCookie("username", score, 365 ); 	
    	
    	newWindow = window.open("")
        newWindow.focus()
        var newContent = "<HTML><HEAD><TITLE>Results</TITLE></HEAD>"
        newContent += "<BODY BGCOLOR='#FFFFFF'><H1>Below you can see how well you did</H1>"
        newContent += texts + " " + score;
    	newContent += " out of ten ";
    	newContent += "</BODY></HTML>"
    	
    	
    	
        newWindow.document.write(newContent)
        newWindow.document.close()
    	
    	
        return false;
    }
    
    function getCookie( cName )
    {
     var v;
    
     return decodeURIComponent( ( v = ( document.cookie || "" ).match( "(^|\\s)" + cName + "=([^;$]+)" ) ) ? v[ 2 ] : "" );
    }
    
    
    function setCookie(c_name,value,exdays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    }
    
    function checkCookie( )
    {
     var lastScore = getCookie("username");
    
     if ( lastScore )
       document.getElementById("feedback").innerHTML = " last time you scored " + lastScore;
    }
    
    </script>
    
    </head>
    <body >
    
    <div class="topbar"></div>
    
    <div class="navbar">
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="Articles.html">Articles</a></li> 
      <li><a class="current" href="Questionaire.html">Questionaire </a></li>  
      <li><a href="Contact.html">Contact </a></li>
      
      
      
      </ul>
     </div>
      <div class ="thedate">
     <SCRIPT>
    document.write(Date()+".")
    </SCRIPT>
    </div>
    <div class="contentbox"> <p style="text-align:center;">Hello and Welcome to the internet blog <br>
    I hope you have read and learnt everything her is the questionaire where we will ask questions about the articles on the site
    <br>
    <br>
    
    <br>
    Have a go below 
    <div id="feedback">its your first time </div>
    
    <script type = 'text/javascript' >
    checkCookie();
    </script>
    
    <br>
    <br>
    <br>
    
    
     <table>
     <form name="Questionaire"  method="post" enctype="text/plain" onsubmit="return results( this )"> 
       <tr>
       <td colspan="6"> 
          <span class="questions"> How many candidates were there in website of the year? </span>
       </td>	
       </tr>   
       
       <tr>
       <td colspan="2">10         :<input type="radio" name="group1" value="1"></td>
       <td colspan="2">100        :<input type="radio" name="group1" value="2"></td>
       <td colspan="2">255        :<input type="radio" name="group1" value="3"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">On what three things did the voters vote? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td>Content         :<input type="checkbox" name="group2" value="content"></td>
       <td>Navigation      :<input type="checkbox" name="group2" value="navigation"></td>
       <td>Code clarity    :<input type="checkbox" name="group2" value="code"></td>
       <td>Number of users :<input type="checkbox" name="group2" value="users"></td>
       <td>Design          :<input type="checkbox" name="group2" value="design"></td>
       <td>Number of pages :<input type="checkbox" name="group2" value="pages"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What does HTML stand for? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">High Text Markup Language         :<input type="radio" name="group3" value="High Text Markup Language "></td>
       <td colspan="2">Hyper Text Markup Language         :<input type="radio" name="group3" value="Hyper Text Markup Language"></td>
       <td colspan="2">Hyper Text Makeup  Language        :<input type="radio" name="group3" value="Hyper Text Makeup Language"></td>
       </tr>
     
     
      <tr>
       <td colspan="6"> 
        <span class="questions">Who maintains HTML? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="6">(non-case sensitive)         :<input type="text" name="group4" value="Enter Answer Here" size="20" ></td>
       </tr>
      
       <tr>
       <td colspan="6"> 
        <span class="questions">When did the internet start (In its most primitive form)? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">40's         :<input type="radio" name="group5" value="40's"></td>
       <td colspan="2">50's         :<input type="radio" name="group5" value="50's"></td>
       <td colspan="2">60's       :<input type="radio" name="group5" value="60's"></td>
       </tr>
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What Network Led the development of the Internet? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">Stormbreaker        :<input type="radio" name="group6" value="stormbreaker"></td>
       <td colspan="2">Titanus-Connect         :<input type="radio" name="group6" value="titanus"></td>
       <td colspan="2">ARPANET       :<input type="radio" name="group6" value="arpanet"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">when was it commercialised? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="6">Choose the Year         :<select name="group7">
    <option value="1980">1980</option>
    <option value="1981">1981</option>
    <option value="1982">1982</option>
    <option value="1983">1983</option>
    <option value="1984">1984</option>
    <option value="1985">1985</option>
    <option value="1986">1986</option>
    <option value="1987">1987</option>
    <option value="1988">1988</option>
    <option value="1989">1989</option>
    <option value="1990">1990</option>
    <option value="1991">1991</option>
    <option value="1992">1992</option>
    <option value="1993">1993</option>
    <option value="1994">1994</option>
    <option value="1995">1995</option>
    <option value="1996">1996</option>
    <option value="1997">1997</option>
    <option value="1998">1998</option>
    <option value="1999">1999</option>
    </select></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">what was removed to remove the restriction on use of the internet? </span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="2">End of the cold war        :<input type="radio" name="group8" value="End of the cold war"></td>
       <td colspan="2">A Patent         :<input type="radio" name="group8" value="A Patent"></td>
       <td colspan="2">NSFNET       :<input type="radio" name="group8" value="NSFNET"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">Are all networks accessible?</span>
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="3">Yes        :<input type="radio" name="group9" value="Yes"></td>
       <td colspan="3">NO         :<input type="radio" name="group9" value="No"></td>
       </tr>
       
       
       <tr>
       <td colspan="6"> 
        <span class="questions">What is the Internet made of?</span> 
       </td>	
       </tr>
       
       
       <tr>
       <td colspan="3">A Lot of  Large Networks  interconnected      :<input type="radio" name="group10" value="Large Networks"></td>
       <td colspan="3">A lot of small and large networks interconnected         :<input type="radio" name="group10" value="Small and large networks"></td>
       </tr>
       
       
      <tr> 
        <td colspan="3">  <INPUT TYPE="reset"></td>
    	<td colspan="3">  <INPUT TYPE="submit" Value="submit"></td>
      </tr>	
     </FORM> 
     </table>
    
    <br>
    <br>
    
    
    
     </p></div>
    
    <div class="tablepos">
    	<table font="verdana" margin-top="100px" align="right" border="1" bordercolor="#333333" style="background-color:#666666" width="180" cellpadding="3" cellspacing="0">
    	<tr>
    		<td><a href="articles.html">Articles</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/About.html">About the Net</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/History.html">History of the Net</a></td>
    	</tr>
    	<tr>
    		<td><a href="Articles/HTML.html">HTML code</a></td>
    	</tr>
    	<tr>
    		<td><a href="articles/Arsenal.html">Arsenal </a></td>
    	</tr>
    </table> 
    
    </div>
    
    
    
    <div class="copyfooter"> </div>
    
    
    </body>
    </html>

  5. Users who have thanked Logic Ali for this post:

    livewires (Aug 11th, 2011)

  6. #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks I had a look through your alterations and saw what went wrong , thanks you helped me a lot


 

Posting Permissions

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