Solved

Using VBScript variables to affect HTML

Posted on 1998-10-23
2
204 Views
Last Modified: 2010-04-09
Why do I not seem to be able to access in the <BODY> of the page, VBScript variable values (initialized and assigned values from a temporary cookie in the <HEAD>)?  I want to read cookie values and use that data to create an ADO recordset from an Access database, and in turn use the returned database info to determine which GIFs get loaded by the HTML page, and I'd like to do all this when the page is first loaded.

Some code parts....
<HEAD>
<SCRIPT LANGUAGE="vbscript">
Alert document.cookie  'temporary - for diagnostics
Dim ImagesOrdered()
Dim numImagesOrdered
Dim strCookie
If InStr(Document.Cookie, "=ordered") = 0 Then
      'the cookie has no images ordered
      Alert ("Your cookie is empty!")
Else
      'the cookie has images ordered
      DigestCookie
End if
Sub DigestCookie
      Dim strCookie
      Dim Cookie(1000)
      strCookie = Document.Cookie
      'chop cookie into pieces
      i = 0
      numCookies = 0
      Do While i <= 999
            cookieLength = Len(strCookie)
            endCookie = InStr(strCookie, ";") - 1
            If endCookie = -1 Then
                  i = 1000                              'i.e. exit loop
            Else
                  'each pair of cookie variable names and values is placed in an array element
                  'cookie(0) is reserved for ImageCartCapacity
                  'cookie(1) is reserved for numImagesOrdered (i.e. num of values = "ordered")
                  Cookie(i) = Left(strCookie, endCookie)
                  strCookie = Right(strCookie, cookieLength - endCookie - 1)
                  i = i + 1
                  numCookies = numCookies + 1
            End if
      Loop
      'determine Image Cart Capacity
      If InStr(Cookie(0), "ImageCartCapacity") = 0 Then
            Alert ("Your ImageCart is empty!  You can only review your order if you have ordered some images during this visit.")
      Else
            cookieLength = Len(Cookie(0))
            startposnValue = InStr(Cookie(0), "=")
            ImageCartCapacity = Right(Cookie(0), cookieLength - startposnValue)
                                          Alert ("ImageCartCapacity=" & ImageCartCapacity)
      End if
      'determine number of cookies for ordered images only
      If InStr(Cookie(1), "numImagesOrdered") = 0 Then
            Alert ("Your ImageCart is empty!  You can only review your order if you have ordered some images during this visit.")
      Else
            cookieLength = Len(Cookie(1))
            startposnValue = InStr(Cookie(1), "=")
            numImagesOrdered = Right(Cookie(1), cookieLength - startposnValue)
                                          Alert ("numImagesOrdered=" & numImagesOrdered)
      End if
      'reset arrays to proper size
      Redim Preserve Cookies(numCookies)
      Redim ImagesOrdered(numImagesOrdered)
      'populate ImagesOrdered array from relevant cookies
      i = 0
      j = 0
      Do While i <= numCookies
            If InStr(Cookie(i), "not ordered") = 0 And InStr(Cookie(i), "ordered") <> 0 Then
                  cookieLength = Len(Cookie(i))
                  startposnValue = InStr(Cookie(i), "=")
                  j = j + 1
                  ImagesOrdered(j) = Mid(Cookie(i), 7, cookieLength - 14)
            End if
            i = i + 1
      Loop
End function
...
</SCRIPT>
</HEAD>

<BODY>
...
create a strSQL out of VBScript variables, down here in the body, and execute it to get a recordset called rsPhotos
OH AND BY THE WAY, THIS ISN'T WORKING
...

<TABLE>
<% For i = 1 to numImagesOrdered %>  
//THAT DOESN'T WORK - WHY NOT?
<tr>      <td>
<INPUT TYPE="radio" NAME="ordered1" VALUE="1">Yes<BR>
<INPUT TYPE="radio" NAME="ordered1" VALUE="0">No</td>
<td ALIGN="center">
      nnn</td>
<td ALIGN="center">
      colour swatch</td>
<td ALIGN="center">
<IMG SRC="<%response.write(rsPhotos("Image_ID"))%>.gif"
      ALIGN="center" BORDER="0" WIDTH="25%"></td>
<td ALIGN="left">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
        Description</FONT></strong></td>
<td ALIGN="CENTER">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
      Legal Considerations</FONT></strong></td>
<td ALIGN="CENTER">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
      Image Format(s)</FONT></strong></td></tr>
<% Next %>
</table>

I expect the problem has something to do with the sequence in which things happen, but I don't know that sequence, or how to change it or take advantage of it.  Help?
0
Comment
Question by:sknippel
2 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 100 total points
ID: 1838978
You are mixing client-side script with server-side script. ASP is processed serverside, for example this:

<% For i = 1 to numImagesOrdered %>

And this is processed before any code is send to the browser.
the variable numImagesOrdered is not initialized server-side (so presumed zero).
The solution is to use serverside script to read the cookie.

<%
var = Request.Cookies("name")
%>

0
 

Author Comment

by:sknippel
ID: 1838979
Thanks greatly.  One last question:
Is it possible to access cookie variables without knowing their names?  That is, can one simply specify something like:

<% var=Request.Cookies(2) %>

to get the second (or third, assuming 0 counts as the first one) cookie variable's value?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spacing 5 57
uploading image to a profile page 1 29
Popup to change image and store url in database 2 31
Safari On Gmail only html 12 37
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now