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? |