Handling Cookies - Advanced example

blue line

Enter the following data to register as guest:

Your first name:
Your last name:
Your company:
Your e-mail:
   
blue line

In this example, the form invokes a CGI, program cookie1 in library js2. The CGI

  1. checks that all the four input fields are filled in
  2. if some input missing, would resend the form with the previous inputs, along with an error message
  3. if no input missing, would resend the form with all the input fields cleared out

Note that the form has two buttons:

  1. the button "Send" is the normal button you would use to submit the form. Our suggestion is that you use this button two or three times, before trying the next button
  2. the button "Set cookie and send" will execute a JavaScript procedure performing the following:
    1. create on the client a cookie (named cookie1) containing the four input fields (if a previous cookie with the same name exists on the client, it will replace it with this new one)
    2. submit the form, thus invoking CGI cookie1 in library js2.
    This JavaScript procedure provides an example of how to write a cookie.

When this page loads, a JavaScript procedure is executed. This procedure checks whether the client has a cookie named cookie1.
If so, it retrieves from the cookie the values of the four input fields and copies such values into the fields of the form.
This Javascript procedure provides an example of how to read a cookie.

Therefore, on a given client, once cookie1 is created/maintained, the form will always display the input fields with initial values taken from the last "Set cookie and send".

Using a cookie to retain user input is a very common approach in the WEB. The information retained

  • is on the client, not on the server (host)
  • can be retransmitted to the server via some JavaScript (usually cookie data are copied into hidden fields of a form)

Should you want to know more about our cookie JavaScript procedure