Hi folks,

The assignment only involves introducing the student to using cookies and a session cookie is used in the exercise, though the student could choose to add an expiration if they desired.

The html code was not included because it was not necessary. The form only passes one variable via post from a selection of items displayed with a radio button, so only one item can be selected at a time. The user then uses their back button to select another item and submit it to the script.

I know there are better ways to write the script but this is only to be a basic exercise.

If you look at the code above, the order events is:

1. declare variables
2. assign input item to variable
3. retrieving cookie
4. adding purchase to cookie
5. create cookie

Now, logically, it would make sense that you would create the cookie first before retrieving it. But, if we put the code to the create cookie before the code to retrieve the cookie, the script only displays the item currently selected from the form. If we use the code as is, the item selected is kept in a session cookie and is printed out as well as other items selected when we use the back button to select another item.

So, the question is, when a person uses the form for the very first time, there is no session cookie because it hasn't been created. Yet the code only works if we "retrieve" the cookie and then create it.

Any ideas as to why it works this way?


In reply to Re^3: Question regarding CGI and cookies by Anonymous Monk
in thread Question regarding CGI and cookies by JOT007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.