Redirecting and Reading Cookie Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LayneMitch via WebmasterKB.com

    Redirecting and Reading Cookie Problem

    I'm supposed to develop a page that asks info as form values and when you hit
    "submit" it takes you to a page that reads the values you entered into the
    first page and displays those values in a message. I can't seem to find where
    I'm going wrong. Here's the code:

    Setting Cookie and Form Page:

    <html><head><ti tle>Problem3</title>
    <script type="text/javascript" src="core.js"></script>
    <script type="text/javascript">

    var FormProblem=
    {
    init: function()
    {
    var the_button=docu ment.getElement ById("mybutton" );

    Core.addEventLi stener(the_butt on, "click", FormProblem.set Cookie);
    Core.addEventLi stener(the_butt on, "click", FormProblem.Red irect);

    },

    setCookie: function()
    {

    var name = document.getEle mentById("first field").value;
    var age = document.getEle mentById("secon dfield").value;
    var color = document.getEle mentById("third field").value;
    var the_cookie = "username:n ame/userage:age/favcolor:color; path=..
    /newpage.html;"
    document.cookie = "my_cookie="+es cape(the_cookie );
    },

    Redirect: function()
    {
    window.location .href="newpage. html"
    }

    };

    Core.start(Form Problem)
    </script></head>
    <body>
    <form id="youridentit y">
    <label>Your Name:</label>
    <input type="text" name="yourname" id="firstfield" <br>
    <label>Your Age:</label>
    <input type="text" name="yourage" id="secondfield "<br>
    <label>Your Favorite Color:</label>
    <input type="text" name="favcolor" id="thirdfield" <br>
    <input type="button" value="submit" id="mybutton">
    </form>
    </body>
    </html>

    newpage.html:

    <html><head><ti tle>NewPage</title>
    <script type="text/javascript">

    function readCookie (the_info)
    {
    if (document.cooki e)
    {
    var the_cookie = document.cookie ;
    var the_cookie = unescape(the_co okie);
    var broken_cookie = the_cookie.spli t("mycookie=" );
    var the_values = broken_cookie[1];
    var broke_again = the_values.spli t("/");

    var property_values ="";

    for (var loop=0; loop < broke_again.len gth; loop++)
    {
    var property_values = broke_again[loop];
    var broken_info = property_values .split(":");
    var the_property = broken_info[0];
    var the_value = broken_info[1];
    the_info[the_property] = the_value;
    }
    }

    // Return the info you got passed
    return the_info;

    }

    var cookie_info = {};
    cookie_info = readCookie(cook ie_info);

    </script></head>
    <body>

    <script type = "text/javascript">

    document.write( "The was someone named"+cookie_i nfo[username]+"at
    age"+cookie_inf o[userage]+"who loved the color"+cookie_i nfo[favcolor]);

    </script>

    </body>
    </html>

    Any idea of where I'm going wrong? I know it's gotta be something simple..
    which'll just make me upset that I couldn't find it.

    --
    Message posted via WebmasterKB.com


  • LayneMitch via WebmasterKB.com

    #2
    Re: Redirecting and Reading Cookie Problem

    Sorry about that:

    Here's the update version of the setCookie() function:

    setCookie: function()
    {

    var name = document.getEle mentById("first field").value;
    var age = document.getEle mentById("secon dfield").value;
    var color = document.getEle mentById("third field").value;
    var the_cookie = "username:" + name + "/userage:" + age + "/favcolor:" +
    color;
    var the_cookie = escape(the_cook ie);
    var the_cookie = the_cookie + "path=/;";
    document.cookie = "my_cookie="+th e_cookie;
    },

    --
    Message posted via WebmasterKB.com


    Comment

    • Stevo

      #3
      Re: Redirecting and Reading Cookie Problem

      LayneMitch via WebmasterKB.com wrote:
      I'm supposed to develop a page that asks info as form values and when you hit
      "submit" it takes you to a page that reads the values you entered into the
      first page and displays those values in a message. I can't seem to find where
      I'm going wrong. Here's the code:
      Might help if you describe what the problem is. All you've said is what
      you hope to achieve, and that you haven't achieved it.

      Comment

      • SAM

        #4
        Re: Redirecting and Reading Cookie Problem

        LayneMitch via WebmasterKB.com a écrit :
        I'm supposed to develop a page that asks info as form values and when you hit
        "submit" it takes you to a page that reads the values you entered into the
        first page and displays those values in a message. I can't seem to find where
        I'm going wrong. Here's the code:
        >
        Setting Cookie and Form Page:
        <html><head><ti tle>Problem3</title>
        <script type="text/javascript">

        var FormProblem=
        {
        init: function()
        {
        alert('init');
        var the_button=docu ment.getElement ById("mybutton" );
        the_button.oncl ick = function() {FormProblem.se tCookie();}
        },

        setCookie: function()
        {
        var name = document.getEle mentById("first field").value;
        var age = document.getEle mentById("secon dfield").value;
        var color = document.getEle mentById("third field").value;
        var the_cookie = "username:"+nam e+
        ",userage:"+age +
        ",favcolor:"+co lor+
        ";path=/";
        document.cookie = "my_cookie="+es cape(the_cookie );
        this.Redirect() ;
        },

        Redirect: function()
        {
        alert('redirect ');
        window.location .href="newpage. html"
        }

        };

        window.onload = function() { FormProblem.ini t(); }

        </script></head>
        <body>
        <form id="youridentit y" onsubmit="retur n false;">
        <label>Your Name:</label>
        <input type="text" name="yourname" id="firstfield" <br>
        <label>Your Age:</label>
        <input type="text" name="yourage" id="secondfield "<br>
        <label>Your Favorite Color:</label>
        <input type="text" name="favcolor" id="thirdfield" <br>
        <input type="button" value="submit" id="mybutton">
        </form>
        </body>
        </html>
        newpage.html:
        <html><head><ti tle>NewPage</title>
        <script type="text/javascript">

        if (document.cooki e)
        {
        var the_cookie = document.cookie ;
        the_cookie = unescape(the_co okie);
        var the_values = the_cookie.spli t("my_cookie= ")[1];
        the_values = the_values.spli t(";")[0];
        the_values = the_values.spli t(',');

        the_cookie = {};
        for(var i=0, n=the_values.le ngth; i<n; i++) {
        var e = the_values[i].split(':');
        the_cookie[e[0]] = e[1];
        }
        }

        </script></head>
        <body>

        <script type = "text/javascript">

        document.write( "There was someone named: "+the_cookie.us ername+
        "<br>at age: "+the_cookie.us erage+
        "<br>who loved the color: "+the_cookie.fa vcolor
        );

        </script>

        </body>
        </html>

        --
        sm

        Comment

        • LayneMitch via WebmasterKB.com

          #5
          Re: Redirecting and Reading Cookie Problem

          SAM wrote:

          Thank you for responding. I'm breaking down your code and this is what I've
          come up with:
          setCookie: function()
          {
          var name = document.getEle mentById("first field").value;
          var age = document.getEle mentById("secon dfield").value;
          var color = document.getEle mentById("third field").value;
          var the_cookie = "username:"+nam e+
          ",userage:"+age +
          ",favcolor:"+co lor+
          ";path=/";
          document.cookie = "my_cookie="+es cape(the_cookie );
          this.Redirect() ;
          },
          Okay, so far the difference in my code and yours is that I'm using event
          listeners and you aren't. This section is different because of the use of
          ";path=/";
          So I incorporated that.
          >
          >window.onloa d = function() { FormProblem.ini t(); }
          >
          ></script></head>
          ><body>
          ><form id="youridentit y" onsubmit="retur n false;">
          > <label>Your Name:</label>
          > <input type="text" name="yourname" id="firstfield" <br>
          > <label>Your Age:</label>
          > <input type="text" name="yourage" id="secondfield "<br>
          > <label>Your Favorite Color:</label>
          > <input type="text" name="favcolor" id="thirdfield" <br>
          > <input type="button" value="submit" id="mybutton">
          ></form>
          ></body>
          ></html>
          Nothing really different here. I'm using Firebug (which I haven't fully
          figured how to use yet..I'm new), and it's saying that the cookies are being
          set once newpage.html is loaded. It's just not reading the cookie for
          whatever the reason is. So..moving on...
          >newpage.html :
          >
          ><html><head><t itle>NewPage</title>
          ><script type="text/javascript">
          >
          >if (document.cooki e)
          {
          var the_cookie = document.cookie ;
          the_cookie = unescape(the_co okie);
          var the_values = the_cookie.spli t("my_cookie= ")[1];
          the_values = the_values.spli t(";")[0];
          the_values = the_values.spli t(',');
          >
          the_cookie = {};
          for(var i=0, n=the_values.le ngth; i<n; i++) {
          var e = the_values[i].split(':');
          the_cookie[e[0]] = e[1];
          }
          }
          >
          ></script></head>
          The only difference here is that because you used ";path=/"; I now have to
          split by a ";".
          ><body>
          >
          ><script type = "text/javascript">
          >
          >document.write ("There was someone named: "+the_cookie.us ername+
          "<br>at age: "+the_cookie.us erage+
          "<br>who loved the color: "+the_cookie.fa vcolor
          );
          >
          ></script>
          Okay...this is where the problem lies. I'm referencing the value of the
          cookies by using strings in cookie_info[username], and the browser isn't
          displaying anything. I just tried using your 'object.method' example and the
          browser displayed the sentence, but each value was 'undefined'...w hy is that?

          There shouldn't be any reason for that. I just had the same issue a week ago
          with cookies and the solution was that if I 'return' the array of info into
          an object that calls 'readCookie()' - which I did in

          var the_property = broken_info[0];
          var the_value = broken_info[1];
          the_info[the_property] = the_value;
          }
          }

          // Return the info you got passed
          return the_info;


          I should be able to reference the value by 'object[string]= value'. What's
          the problem with this solution know?

          --
          Message posted via WebmasterKB.com


          Comment

          • LayneMitch via WebmasterKB.com

            #6
            Re: Redirecting and Reading Cookie Problem

            Sorry about that. Hopefully you can follow this last post.

            Firebug got in the way and screwed up my message so your parts of the code I
            was responding to somehow got deleted.

            --
            Message posted via http://www.webmasterkb.com

            Comment

            • SAM

              #7
              Re: Redirecting and Reading Cookie Problem

              LayneMitch via WebmasterKB.com a écrit :
              SAM wrote:
              >
              Thank you for responding. I'm breaking down your code and this is what I've
              come up with:
              >
              > setCookie: function()
              > {
              > var name = document.getEle mentById("first field").value;
              > var age = document.getEle mentById("secon dfield").value;
              > var color = document.getEle mentById("third field").value;
              > var the_cookie = "username:"+nam e+
              > ",userage:"+age +
              > ",favcolor:"+co lor+
              > ";path=/";
              > document.cookie = "my_cookie="+es cape(the_cookie );
              > this.Redirect() ;
              > },
              >
              Okay, so far the difference in my code and yours is that I'm using event
              listeners and you aren't.
              I don't because I Haven't 'core.js' :-)

              This section is different because of the use of ";path=/";
              It seam to me you did too, no ?

              I use ',' as separator in the_cookie's value
              So I incorporated that.
              >window.onloa d = function() { FormProblem.ini t(); }
              If you did keep your core-functions, not sure that could work.
              >>
              ></script></head>
              ><body>
              ><form id="youridentit y" onsubmit="retur n false;">
              > <label>Your Name:</label>
              > <input type="text" name="yourname" id="firstfield" <br>
              > <label>Your Age:</label>
              > <input type="text" name="yourage" id="secondfield "<br>
              > <label>Your Favorite Color:</label>
              > <input type="text" name="favcolor" id="thirdfield" <br>
              > <input type="button" value="submit" id="mybutton">
              ></form>
              ></body>
              ></html>
              >
              Nothing really different here.
              Yes it's copy-paste.
              I'm using Firebug (which I haven't fully
              figured how to use yet..I'm new), and it's saying that the cookies are being
              set once newpage.html is loaded. It's just not reading the cookie for
              whatever the reason is. So..moving on...
              If cookies exist they are surely read.
              >>newpage.htm l:
              ><html><head><t itle>NewPage</title>
              ><script type="text/javascript">
              >>
              >if (document.cooki e)
              > {
              > var the_cookie = document.cookie ;
              > the_cookie = unescape(the_co okie);
              > var the_values = the_cookie.spli t("my_cookie= ")[1];
              > the_values = the_values.spli t(";")[0];
              > the_values = the_values.spli t(',');
              Notice here how I set the object from the_values
              (instead giving that to something I don't understand)
              > the_cookie = {};
              > for(var i=0, n=the_values.le ngth; i<n; i++) {
              > var e = the_values[i].split(':');
              > the_cookie[e[0]] = e[1];
              > }
              > }
              >>
              ></script></head>
              >
              The only difference here is that because you used ";path=/"; I now have to
              split by a ";".
              then I split by ',' and
              and I fill-up the object from this array 'the_values'
              the_cookie[aNewName] = aNewValue;
              ><body>
              >>
              ><script type = "text/javascript">
              >>
              >document.write ("There was someone named: "+the_cookie.us ername+
              > "<br>at age: "+the_cookie.us erage+
              > "<br>who loved the color: "+the_cookie.fa vcolor
              > );
              >>
              ></script>
              >
              Okay...this is where the problem lies. I'm referencing the value of the
              cookies by using strings in cookie_info[username], and the browser isn't
              displaying anything.
              document.write( "The was someone named: "+the_cooki e['username']);
              works fine in my browser.
              I just tried using your 'object.method' example and the
              browser displayed the sentence, but each value was 'undefined'...w hy is that?
              If you have not added ';path/' to the cookie, I think you have to do :

              function readCookie ()
              {
              if (document.cooki e)
              {
              var the_cookie = document.cookie ;
              the_cookie = unescape(the_co okie);
              the_cookie = the_cookie.spli t("=")[1]; // split by '='
              var the_values = the_cookie.spli t("/");

              var the_info = {}; // here we build a new object

              for (var loop=0; loop < the_values.leng th; loop++)
              {
              var property_values = the_values[loop].split(":");
              var the_property = property_values[0];
              var the_value = property_values[1];
              the_info[the_property] = the_value;
              }

              // Return the info you got passed
              return the_info;
              }
              }

              var cookie_info = readCookie();


              --
              sm

              Comment

              • LayneMitch via WebmasterKB.com

                #8
                Re: Redirecting and Reading Cookie Problem

                SAM wrote:
                document.write( "The was someone named: "+the_cooki e['username']);
                >works fine in my browser.

                Got it. This was a syntax error. I didn't have the single quotations
                before/after variables.
                >If you have not added ';path/' to the cookie, I think you have to do :
                >
                >function readCookie ()
                >{
                if (document.cooki e)
                {
                var the_cookie = document.cookie ;
                the_cookie = unescape(the_co okie);
                the_cookie = the_cookie.spli t("=")[1]; // split by '='
                Got it. This was another one of my errors.
                var the_values = the_cookie.spli t("/");
                >
                var the_info = {}; // here we build a new object
                >
                for (var loop=0; loop < the_values.leng th; loop++)
                {
                var property_values = the_values[loop].split(":");
                var the_property = property_values[0];
                var the_value = property_values[1];
                the_info[the_property] = the_value;
                }
                >
                // Return the info you got passed
                return the_info;
                }
                >}
                >
                >var cookie_info = readCookie();
                >
                Thanks for your expertise. The problem has been solved. I basically kept my
                code and saw that I needed the quotations for the cookie_name['username']...
                etc. Also I did use ;path=/ originally, but I found a better solution by
                using "path=newpage.h tml;". Once I made these changes plus the one
                immediately above, everything worked.

                I do like your code because it's showing me a different view without the file,
                which in turn is showing me a more abstract view about the use of objects and
                methods(somethi ng different than textbook versions). :-)

                Thanks.

                --
                Message posted via http://www.webmasterkb.com

                Comment

                • Bart Van der Donck

                  #9
                  Re: Redirecting and Reading Cookie Problem

                  SAM wrote:
                  LayneMitch via WebmasterKB.com a écrit :
                  >
                  >...
                  >I'm using Firebug (which I haven't fully figured how to use yet..I'm
                  >new), and it's saying that the cookies are being set once newpage.html
                  >is loaded. It's just not reading the cookie for whatever the reason is.
                  >
                  If cookies exist they are surely read.
                  Sorry for the nitpick, but there are actually various reasons why a
                  cookie could not be read. They could be disabled, or set as limitedly
                  accessible by the browser. A cookie can also be expired (or non-
                  persistent), and domain name and/or path restrictions could apply.

                  I think the following is an excellent article:


                  --
                  Bart

                  Comment

                  Working...