The updateNewUser.asp Form in Action

As soon as a new user clicks the Submit button, this form is displayed. However, the new cookie is written before the form is displayed. That is why the user would see the cookie alert before this form is displayed. When the updateNewUser form is displayed, the user information has been written to the database, a new unique ID has been generated, and the cookie has successfully been written to the user's browser!

As mentioned above, whenever we read or write cookies, this must be done before any HTML code is generated.

Try It Out - Showing New Users They Have Been Entered into the Database

1.  OK, this is the last form. Fire up Notepad.exe and create a new file called updateNewUser.asp. Be sure to save this in \Chapter13 with the rest of the ASP and DLL files. Now please enter this code as shown:

<%
Dim myDll
Dim myArray 
Dim cookieID
dim firstName
dim lastName

firstName = Request.Form("firstName")
lastName = Request.Form("lastName")

Set myDll = Server.CreateObject("trackVisitors.visitors")

cookieID = myDll.setvisitor(firstName, lastName)

Response.Cookies("visitorNumber") = cookieID
Response.Cookies("visitorNumber").Expires = "December 30, 2000"

%>

<HTML>
<TITLE>Database Programming with Visual Basic 6.0</TITLE></HEAD>

<CENTER>
<H1><font size=4>Updating New User</font></H1>
<H2>Database Programming with Visual Basic 6.0</H2><BR>
</CENTER>
<B>
Welcome to my site
  <% Response.write(firstName)
     Response.Write(" ")
     Response.Write(lastName) %> 
     <P>
     <%
     Response.Write(Request.Cookies("visitorNumber"))
     Response.Write (" is the cookie just written to your system.")
  %>
<P>
This is your first visit on <%=now %>
</B>
<HR>
<h5>Copyright: Programming Databases with Visual Basic 6.0.<BR>
Last revised: May 24, 1998</H5>
</HTML>

How It Works

Within the % VBScript tags, we first extract the information from the firstName and lastName text boxes on the newUser form and assign them to local variables. Recall that when that form was posted, it called the updateNewUser.asp form. So by using the .Form method of the Request object and passing it the name of the field we are interested in, we can retrieve that value. So we just grab the first and last name from the text boxes in the newUser.asp form.

firstName = Request.Form("firstName")
lastName = Request.Form("lastName")

Here we use the DLL that manages all of our database access. We instantiate our DLL and assign it to our object variable myDll:

Set myDll = Server.CreateObject("trackVisitors.visitors")

Once we have created an instance of our visitors.dll, we call its .setVisitor method and pass in the firstName and lastName as parameters. This is, of course, the name of the user that just filled out the newUser form. We call setVisitor for new users. This method updates the database and returns a unique number that we will use to write the cookie. This unique value is assigned to the variable cookieID:

cookieID = myDll.setvisitor(firstName, lastName)

We pass in the first and last name of the surfer to the .setVisitor method of our DLL. The DLL updates the database and return back to us the unique cookieId.

When the new user has been written to the database, we then write the cookie to the user's PC. We give it the value of cookieID which in our example is "7". We then set the .Expires property so the cookie does not disappear when the user leaves our site. If we did not set the .Expires property, the cookie would only last for the session. So we place it there to last for a while:

Response.Cookies("visitorNumber") = cookieID
Response.Cookies("visitorNumber").Expires = "December 30, 2000"

After the VBScript code executes, we jump into the HTML code and display the results. Of course, we bracket our VBScript code within the HTML code in <% %> as usual:

<B>
Welcome to my site
  <% Response.write(firstName)
     Response.Write(" ")
     Response.Write(lastName) %> 
     <P>
     <%
     Response.Write(Request.Cookies("visitorNumber"))
     Response.Write (" is the cookie just written to your system.")
  %>

© 1998 by Wrox Press. All rights reserved.